Reputation: 395
Suppose I have a list
a = ['Jane\t10','Mike\t40','Lance\t20','Kirk\t30']
I want to make list above into a list of list. So it will be:
a = [['Jane', 10], ['Mike', 40], ['Lance', 20], ['Kirk', 30]]
Already doing this:
for i in a:
i = i.split('\t')
But when I print(a)
the result haven't change. Is there any way to do so without create a new variable ?
Upvotes: 1
Views: 46
Reputation: 1
a = ['Jane\t10','Mike\t40','Lance\t20','Kirk\t30']
new_a = [ ]
for i in a:
i = i.split('\t')
new_a.append(i)
print (new_a)
dude,figthing..
Upvotes: 0
Reputation: 26039
Use split
in a list-comprehension:
a = ['Jane\t10','Mike\t40','Lance\t20','Kirk\t30']
a = [x.split('\t') for x in a]
# [['Jane', '10'], ['Mike', '40'], ['Lance', '20'], ['Kirk', '30']]
Upvotes: 4