+ 25
List comprehension vs. the map function.
Dear Python fans out there: Which do you prefer? As I am progressing towards the course, I became familiar with map function. Now, it is very convenient, it really is, so should I use it instead of list comprehensions whenever I can? Thank you for all your answers in advance.
21 ответ
+ 30
In general it is better to use list comprehension, but there are times when map and filter are better.
https://code.sololearn.com/cqJUKgH6zn1D/?ref=app
+ 8
you can use a for loop and try both of them with 10000 times or more, you will notice that the map funtion is quicker then the list one.
ex:
import timeit
x = timeit.timeit("[str(i) for i in range(100)]", number=10000)
print(x)
y = timeit.timeit("map(str, range(100)), number=10000)
print(y)
+ 8
It is important to compare apples with apples. In Mohamed DZ's example the maps do not result in a list but the list comprehension does. By encapsulating the map in a list both are now doing the same thing.
With the built-in function 'str' map is quicker, but as soon as you use lambda it gets longer and when you start chaining, it gets even worse.
Hence my original statement. In general list comprehension is the correct option.
https://code.sololearn.com/caVGRw4he3EQ/?ref=app
+ 7
Thanks, I also think that list comprehensions look more clean. Map and filter take less typing, so it might seem less work, but...call me crazy for this, since the Guido Van Rossum wanted to create a language with clean straightforward syntax, which makes code easier to understand and maintain, I think that we, have some kind of duty to keep it that way.
+ 7
list compehension creates a list. map maps it.
+ 5
$ python -mtimeit -s'xs=range(10)' 'map(lambda x: x+2, xs)'
100000 loops, best of 3: 4.24 usec per loop
$ python -mtimeit -s'xs=range(10)' '[x+2 for x in xs]'
100000 loops, best of 3: 2.32 usec per loop
: I prefer not to introduce unnecessary names, like o here, and your examples show why.
+ 5
(map) may be microscopically faster in some cases (when you're NOT making a lambda for the purpose, but using the same function in map and a listcomp).
(List comprehensions) may be faster in other cases and most (not all) pythonistas consider them more direct and clearer.
An example of the tiny speed advantage of map when using exactly the same function:
$ python -mtimeit -s'xs=range(10)' 'map(hex, xs)'
100000 loops, best of 3: 4.86 usec per loop
$ python -mtimeit -s'xs=range(10)' '[hex(x) for x in xs]'
100000 loops, best of 3: 5.58 usec per loop
An example of how performance comparison gets completely reversed when map needs a lambda:
$ python -mtimeit -s'xs=range(10)' 'map(lambda x: x+2, xs)'
100000 loops, best of 3: 4.24 usec per loop
$ python -mtimeit -s'xs=range(10)' '[x+2 for x in xs]'
100000 loops, best of 3: 2.32 usec per loop
+ 4
if you want to Learn Python in deep use both of,but if you want to write your code very small use list comprehension.
+ 4
As Louis and Michael Veith pointed out, the main point is that (map) doesn’t make a list. This can be very useful, whether it’s efficient or not. See this stupid example:
https://code.sololearn.com/cCf6DyCZ1f6t/?ref=app
+ 3
It doesn't make much difference for small codes and small applications, it's your choice
as example, to convert user's input to numbers
nums = [int(i) for i in input().split()]
or
nums = map(int,input().split())
whichever you feel better you can use
+ 3
I guess map(), filter(), and companions bring features of functional programming to Python. Just like list comprehension, those features could be achieved with other mon-functional syntax features as well. However, the two are not comparable, as the main goal of a list comprehension is to create a list, allowing all sorts of syntactic manipulation in-between, whereas map(), for instance, is one of those functions that can be used in-between with its main purpose to programmatically modify a given iterable in the same manner, resulting in a map-object.
+ 3
@Mohamed DZ :) if you try to generate a list instead of a map object the result is not so different anymore. And thanks for showing me a new way to use timeit.
import timeit
x = timeit.timeit("[str(i) for i in range(100)]", number=10000)
print(x)
print([str(i) for i in range(100)])
y = timeit.timeit("list(map(str, range(100)))", number=10000)
print(y)
print(list(map(str, range(100))))
Edit: now I saw this was exactly what @Louis did. Thanks for the good program!
+ 2
thnx every body for ur information
+ 2
both ok, but i prefer list comprehension.
+ 2
Yes, speed testing really does shed some light on this, and when speed of your code is more important than the readability and ease of maintenance, map, filter and reduce functions are absolute winners, they are much faster. filter function takes much less time than a for loop that iterates over a list.
+ 2
True, true, now I see it, I missed that and misinterpreted some results. Lambdas make things regarding speed much worse, they make your code slower, not faster. Thank you for thorough explanation, Louis.
+ 2
good tip, thx
+ 1
welcome
+ 1
oh yeah..
+ 1
if you r very familiar, as u say, still use both as they r fundamental part of programming as lists can still be very versatile