Nested maps in Python 3

I want to convert my list list = [" a , 1 ", " b , 2 "]

to a nested list [["a","1"],["b","2"]]

.

The following works:

f1_a = map(lambda x :[t.strip() for t in x.split(',',1)],list)

      

but this doesn't work with Python 3 (it does work with Python 2.7!):

f1_b = map(lambda x :map(lambda t:t.strip(),x.split(',',1)),list)

      

Why is this?

Is there a more concise way than f1_4

to achieve what I want?

+3


source to share


1 answer


Python 3 map

returns an object map

that needs to be explicitly converted to a list:

f1_b = list(map(lambda x: list(map(lambda t: t.strip(), x.split(',', 1))), lst))

      



Although in most cases, you should prefer to list the errors map

:

f1_a = [[t.strip() for t in x.split(',', 1)] for x in lst]

      

+4


source







All Articles