List comprehension vs. the map function. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

16th Mar 2018, 6:10 AM
DianaTheAlien
DianaTheAlien - avatar
21 Answers
+ 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
16th Mar 2018, 6:14 AM
Louis
Louis - avatar
+ 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)
24th Mar 2018, 3:22 PM
Mohammed Chami
Mohammed Chami - avatar
+ 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
24th Mar 2018, 9:09 PM
Louis
Louis - avatar
+ 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.
16th Mar 2018, 6:43 AM
DianaTheAlien
DianaTheAlien - avatar
+ 7
list compehension creates a list. map maps it.
17th Mar 2018, 12:10 PM
Oma Falk
Oma Falk - avatar
+ 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.
20th Mar 2018, 11:49 PM
Oluwaseun Shaydus Oni
Oluwaseun Shaydus Oni - avatar
+ 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
25th Mar 2018, 7:09 PM
Baraa AB
Baraa AB - avatar
+ 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.
17th Mar 2018, 2:08 AM
Maninder $ingh
Maninder $ingh - avatar
+ 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
1st Apr 2018, 2:45 PM
Alex
+ 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
30th Mar 2018, 5:14 AM
Zoetic_Zeel
Zoetic_Zeel - avatar
+ 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.
31st Mar 2018, 5:10 AM
Michael Veith
Michael Veith - avatar
+ 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!
2nd Apr 2018, 7:52 AM
Edward
Edward - avatar
+ 2
thnx every body for ur information
17th Mar 2018, 10:42 AM
Baslael Kifletsion
Baslael Kifletsion - avatar
+ 2
both ok, but i prefer list comprehension.
23rd Mar 2018, 7:29 AM
Erick Mwenda Njagi
Erick Mwenda Njagi - avatar
+ 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.
24th Mar 2018, 8:18 PM
DianaTheAlien
DianaTheAlien - avatar
+ 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.
24th Mar 2018, 10:24 PM
DianaTheAlien
DianaTheAlien - avatar
+ 2
good tip, thx
26th Mar 2018, 7:43 AM
x.teddy
x.teddy - avatar
+ 1
welcome
16th Mar 2018, 3:29 PM
MeMe Boy
MeMe Boy - avatar
+ 1
oh yeah..
20th Mar 2018, 5:48 PM
Arun
+ 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
25th Mar 2018, 3:22 AM
SanJ
SanJ - avatar