JON PANTAU
JON PANTAU

Reputation: 395

Split a list into a list of list

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

Answers (2)

kenny.hou
kenny.hou

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

Austin
Austin

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

Related Questions