Map() Is More Powerful Than You THOUGHT In Python

preview_player
Показать описание
Map() is more powerful than you thought in Python. #Python #Code #Shorts
Рекомендации по теме
Комментарии
Автор

map() is not used often because we can easily do the same thing with a list comprehension

chidvilaskarpenahalli
Автор

Every part of my strong-type-loving brain just screams in agony seeing a string multiplied by a number like that.

MD-vsff
Автор

here is simpler version
values = [1, 2, 3, 4]
print([i*'x' for i in values])

emrecrakoglu
Автор

Before I knew list comprehensions I did some devious nested map functions

nothiiiiiiiing
Автор

For any who's wondering why use this over list comprehension?

You must use map when you know you're using this function a lot.
You can save time by not write a lengthy unreadable comprehension syntax line.
Imagine a function had to be called a 100 times in total, map is better than writing 100 list comprehensions.

Only overhead is converting to list.

TheLogicalHuman
Автор

This is a great feature but I believe this is not the best example because a list comprehension would work better here.

Here's an actually good example:

inp = input("Enter numbers separated by space: ")
numbers = list(map(int, inp.split()))

bettercalldelta
Автор

Why not just `(func(i) for i in iterable)`?
The same also with filter: `(x for x in iterable if func(x))`
The only gain is the use of lambda which can also be used but it is required to create the lambda object every time: `(x for x in iterable if (lambda _: True)(x))`

david_lev
Автор

Just use list/generator comprehesion, it will be more redable

josefkaras
Автор

Why not use a generator expression or list comprehension instead?

sergioamador
Автор

For those that are interested, there are 'cheat sheets' for a number of programming languages, pdf form and just right for printing out, on line. Just Google your request.

jimparsons
Автор

A lot of people pointing out list comprehension instead: yes, in most cases that is fine esp if you are doing list(map(....)), but in some cases you dont need the temporary list made in memory and instead using generators is faster.

hidude
Автор

learned map in racket, cool to know that python has a similar function

elijahsagaran
Автор

[I*'x' For i in values] should be both more efficient and faster to writr

etaizilberman
Автор

You can alternate use a generator...

mapped = (str(i) for i in values)

speakingsarcasm
Автор

How you do the arrow?? And what does it actually signify??

notyouraveragepotato
Автор

This can be done in one line using list comprehension.
Sorry, but I've never found a use for the map function, that can't be done using comprehensions

peterbarraud
Автор

Probably I'm getting this wrong, but I think any language ahas such a map function if somebody writes it and then you import....

lvmbk
Автор

Note that you should convert to a list you store the refrence somewhere when you want to use it more then once.
u=map(lambda n:10*n, range(1, 4))
print(list(u))
print(list(u))
gives:
[10, 20, 30]
[]

oida
Автор

Why not just use a list comprehension?

KalebARoberts
Автор

Here's another 'trick':
_ = *map(print, my_list),

Darakon