Python map() does not work for list.append()

Viewed 4216

Here I am appending items into a list in an iterative manner:

l = []
for i in range(4):
   l.append(i)
print l  # Ans: [0, 1, 2, 3]

Where as, if I use map() to do the same, I get a different result

l = []
map(l.append, range(4))  # Ans: [None, None, None, None]
1 Answers

Python map returns the values returned for each function call in a list (or a generator).

In this case list.append returns None. Also l is mutable, so it should contain the items.

Related