Functional Programming in Python: The 'map()' Function

preview_player
Показать описание

In this Python tutorial you'll discover further basics of functional programming, namely how to use the "map()" function to transform data structures.

We'll take an example data set represented using an immutable data structure from the previous videos in this series, and then we'll create a "transformed" version of the same data using Python's built-in map function.

"map()" is one of the functional programming primitives or building blocks available in Python and it's useful in a number of contexts.

Later in the video you'll also see how the map function relates to list comprehensions and generator expressions and how using them is (arguably) more Pythonic that relying on plain "map()" calls.

This video is part of a series of Python functional programming tutorials that I'm recording so stay tuned for the next instalment.

Be sure to check out these associated articles and tutorials if you want to dive deeper into the subject:

* * *

FREE Python Coding Tutorials & News:
Рекомендации по теме
Комментарии
Автор

I understand the benefits of functional programming, but I also don't want to create a copy of the data just to pass it to a function that is only going to read it.

It would be cool, if python supported "const" which would stop people from editing mutable datastructures.

devmishra
Автор

This is a really good playlist of videos, thanks.

davidblake
Автор

Thanks for your videos and work, Dan. It's very much appreciated

gerozayas
Автор

In functional programming, if you keep making copies of past data when you make a transformation, how do you keep track of manage new variable names so you aren't having a ton of new variables?

JonnyD
Автор

Hi. Nice work. However I think your tutorial here doesn't do justice to the map function, because once you introduce more than 1 iterables that's where the power of map changes in reference to list comprehensions / generators.

So,

tuple(map(lambda: x, y: x*y, [1, 2, 3], [1, 2, 3])) = (1, 4, 9)

How would I do this using a list comprehension / generator expression ?

adnan
Автор

Dan, in a daily basis and regarding to performance, what is the best choice? using list comprehensions vs map function?

angiealejo
Автор

Excellent and concise delivery. Thanks for staying up after a long day - much appreciated

charliec
Автор

Nice vídeo! Perhaps it would be cool showing what happens when passed more than one iterable.

LucasRibeiro
Автор

it would be nice to have the list of scientists to copy/paste from to follow along

czglory
Автор

Hello Dan, I've heard that you pronounce word "tuple" as tOOple not a tAple as I always thought. Is it a correct way to pronounce it?

dvdvdmt
Автор

U r amazing man...need more advanced tutorials using python...

bjugdbjk
Автор

Dan please tell me how you changed your cursor to this round ? I was looking for it but didn’t found

adamkenton
Автор

Nice video. I had a couple of “AHA!” Moments during it. Thanks for the insight.

mattjclay
Автор

You really ramble on compared to some of the other teachers .

expat
Автор

def name(x: Scientist) -> str:
return x.name
"""
>>> names = tuple(map(name, scientists))
>>> names
('Lovelace', 'Noether', 'Curie', 'Youyou', 'Yonath', 'Rubin', 'Ride')
"""

def age(x: Scientist) -> int:
_year_example = 2017
return _year_example - x.born
"""
>>> ages = tuple(map(age, scientists))
>>> ages
(202, 135, 150, 87, 78, 89, 66)
"""

def name_and_age(x: Scientist) -> Tuple[str, int]:
return name(x), age(x)

"""
>>> names_and_ages = tuple(map(name_and_age, scientists))
>>> names_and_ages
(('Lovelace', 202), ('Noether', 135), ('Curie', 150), ('Youyou', 87), ('Yonath', 78), ('Rubin', 89), ('Ride', 66))

chadhovey
Автор

the only thing I got from this video is pprint :(

Haluna
Автор

Um, but Ada Lovelace being 202 years old!? Haha yeah right it's getting late

SusanAmberBruce
welcome to shbcf.ru