Reputation: 145
I was expecting the code under header 'New List with MAP' below to return a list just like the for loop and the list comprehension....but alas, it does not. I get, <map object at 0X000...8C1CD30>
instead. How do i go from this to my list?
New List with Map
list_a = ['one_A', 'two_A', 'three_A']
#initialize the list
new_li = map(lambda i: '{0}\n'.format(i), list_a)
print(new_li)
New List With Loop (Works)
list_a = ['one_A', 'two_A', 'three_A']
new_li = []
for i in list_a:
new_li.append('{i}\n'.format(i))
print(new_li)
New List with List Comprehension (Works)
list_a = ['one_A', 'two_A', 'three_A']
new_li = ['{i}\n'.format(i=i) for i in list_a]
print(new_li)
Upvotes: 3
Views: 48
Reputation: 42778
map
returns an iterator. If you want the values in a list, you have to convert it to a list:
new_li = list(map(lambda i: '{0}\n'.format(i), list_a))
or
new_li = list(map('{0}\n'.format, list_a))
Upvotes: 3