I have a list of sublists in the following format:
[['1023869', '-0.137869328454', '0.318039373746', '1.243\n'], ['1023870', '-0.121434633793', '0.324670024328', '1.243\n']]
I want to delete "\n" from the last element in the sublist.
CodePudding user response:
Use a list comprehension with rstrip
:
[sublist[:-1] [sublist[-1].rstrip()] for sublist in lst]
[['1023869', '-0.137869328454', '0.318039373746', '1.243'],
['1023870', '-0.121434633793', '0.324670024328', '1.243']]
CodePudding user response:
Just mutable last element in given list.
for subList in alist:
subList[-1] = subList[-1][:-1]
>>> alist
>>> [['1023869', '-0.137869328454', '0.318039373746', '1.243'], ['1023870', '-0.121434633793', '0.324670024328', '1.243']]