Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python map() does not work for list.append() [duplicate]

Tags:

python

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]
like image 574
Ibrahim Quraish Avatar asked Oct 15 '25 03:10

Ibrahim Quraish


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.

like image 118
Adam Shem-Ur Avatar answered Oct 16 '25 17:10

Adam Shem-Ur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!