The Map, Filter, and Reduce Functions in Python

preview_player
Показать описание
Need help with your Python project? I can help:

This video introduces the map, filter, and reduce functions in Python.
Рекомендации по теме
Комментарии
Автор

you are amazing thank you for sharing.

MrSimospirit
Автор

Thanks for great video! Why would one use map function over list comprehension. What are the difference?

haneulkim
Автор

I ask for a video about the decorator and the meta class ))

neofit
Автор

Hi, I like your videos and the way you present the subject. But today I think all functions are wrong to use in modern Python.
With numbers = [1, 2, 3, 4, 5]

MAP
List comprehension does the same thing, maybe faster and is more readable.
>>> def square(x):
>>> return x**2
>>> list(map(square, numbers)) # MAP
>>> [1, 4, 9, 16, 25]
>>> [square(x) for x in numbers] # List Comprehension
>>> [1, 4, 9, 16, 25]

>>> list(map(lambda x: x**2, numbers)) # MAP with lambda
>>> [1, 4, 9, 16, 25]
>>> [x**2 for x in numbers] # List Comprehension
>>> [1, 4, 9, 16, 25]
Again... More readable

FILTER
List comprehension again more readable for human eye
>>> list(filter(lambda x: x > 3, numbers)) # FILTER
>>> [4, 5]
>>> [x for x in numbers if x > 3] # List Comprehension
>>> [4, 5]

REDUCE
List comprehension can't be used but as said in one Stack Overflow:
"Removed reduce(). Use functools.reduce() if you really need it; however, 99 percent of the time an explicit for loop is more readable."
It's only for Python2, which is dying breed... If you use it in Python3, it does not exist!
>>> result = 0
>>> for num in numbers:
>>> result += num
>>> result
>>> 15

These are my 2 cents for this subject.
If you think I'm wrong I'll be happy to see in your next video some useful cases where List Comprehension is worse choice.

songokussjcz
Автор

It's interesting that you have to import the reduce function and not map or filter.

sromanski
visit shbcf.ru