Python - List comprehension vs map function TUTORIAL (speed, lambda, history, examples)

preview_player
Показать описание
Python tutorial on the difference between the map() function and list comprehensions. We learn about using map with lambda and list comprehensions with lambda. We run speed test, compare advantages of each, and determine if you should use map or list comprehensions for iterating in Python. We also discuss what Guido prefers.

❗❗This video is part of a SERIES. Check out the other videos below ❗❗

💻 Python List Comprehensions Course Playlist:

💻 Python Interview Questions Playlist:

📖 The complete Udemy course on Python Built-in Functions:

💻 Python filter() built-in function:

💻 Python map() built-in function:

💻 Python list() built-in function:

⚙️ Recommended Computer Gear:

✈️ Recommended Digital Nomad Gear:
Рекомендации по теме
Комментарии
Автор

I thinks you should get more views. you are covering the most important topics and related questions. Thanks for the video

opium
Автор

I think one time to use map over a list comprehension is when you don't actually need a list.
For example, I have a function that takes 24 hour time as a string and converts it to the number of minutes from 00:00 for that time.
The way my function works is to unpack the 24 hour time into two variables hrs and mins which are integers.

Using map (as I have done):

def minutes(str_time):
hrs, mins = map(int, str_time.split(':'))
return hrs*60 + mins


Using list comprehension:

def minutes(str_time):
hrs, mins = [int(x) for x in str_time.split(':')]
return hrs*60 + mins

To me, use of map here is more intuitive and probably faster, although speed isn't really my concern here.
This is my first time using map since I started to learn Python in February 2020.
If I want a list I'll of course use list comprehension because it is generally clearer and doesn't require calling the list function.

ninesquared
Автор

The history and best practices discussion was very helpful

brianglass
Автор

Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

akira_asahi
Автор

May be this way

num = [1, 2, 3, 4]

lst = list(map(str, filter(lambda x: x > 1, num)))

print(lst)

mohammadjunaidmansoori
Автор

Great, you have explained everything.

Rockleev
Автор

Lolol the creator not liking map, goated

jell._.y
Автор

Ok, but how use the map without convert it ? which objects accept a map object ?

Fine_Mouche
Автор

7:37 - it would be better to use filter first and then map ;-)

rb
Автор

Look man ..my English is weak but you have very nice way inserting information in brain... thank you

sehishok
Автор

Hello! What is this editor\high-lighting software? It looks cool

Morexod
Автор

I didn't get why you used map(int, [1, 2, 3, 4]). The list was already int type.

yekhtiari