quantifiedcode / python-anti-patterns

An open collection of Python anti-patterns and worst practices.
https://quantifiedcode.github.io/python-anti-patterns
Other
1.71k stars 249 forks source link

Using map() or filter() where list comprehension is possible #132

Open drsasa opened 5 years ago

drsasa commented 5 years ago

Generally this section is misleading.

I understand what writer wanted to say but its not correct.

map() returns an iterator not list. Therefore in text on few places is mentioned that map() returns list.

Therefore use case for map over list comprehension is let's say bit different and with map can be more memory efficient.

I'm just pointing out, so that section can be bit adjusted, but rest standing.

dmtucker commented 5 years ago

Generator expressions are lazy and (AFAIK) accepted anywhere a map object would be:

>>> ' '.join(map(str, range(5))) == ' '.join(str(i) for i in range(5))
True