Use THIS For Cleaner & More Memory Efficient Code in Python

preview_player
Показать описание
This is much cleaner and more memory efficient code in Python. Today we are looking at the map() function, which can transform the data in your list at an incredible fast speed in a very memory efficient manner.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

Bro, you give a 90 min lecture in 7 minutes. You´re a python beast

senfdame
Автор

One thing you touched on but I feel should be more emphasized is that for lots of simpler operations (e.g. squaring a number), list comprehension is faster due to not needing to look up functions. With my example you could just type [x*x for x in numbers] and would not need to create a lambda function to do the calculation, resulting in a drastic speed increase (in my example over 2x faster)

Also another point that I've seen others address is that map is generally seen as less Pythonic, while you shouldn't necessarily let conventions get in the way of writing a more efficient program, it's worth considering if using a less common feature of the language making it harder to understand is worth the small speed increase, especially considering how slow python is already.

It's good to see people bringing features like the map function up and talking about when they are better/worse, and with performance testing to prove it though.

actuallyatiger
Автор

Generator expressions is probably what you are looking for, it's more pythonic and also doesn't requires to create/import unusual functions (you can use operators directly). For example, if you want to double elements from a list you would simply write "(x*2 for x in your_list)" whereas it would be more complex and harder to read using the map function. In terms of speed I would be surprised if it was significantly different from the map solution.

osquigene
Автор

Excellent! Your Python tutorials are by far the best on the internet. Thank you!

dcollett
Автор

[p.upper() for p in people] is also short.

Not a fan of map(str.upper, people). The good thing about it is that it does a single method resolution, which could be fast, but I would use that only in really critical part of code and if it really brings substantial improvement. In python rarely the case.

I am perfectly fine for standalone functions and callables, like map(int, somelist) for example. There are some other cases where I use map instead of list comprehension, but not super frequent. In the code I wrote, readability is more important than speed (the amount of data I process is small anyway, and whetever it finishes in 50 or 60 ms doesn't matter, it will be fast one way or another, and mostly bottleneck by some network IO anyway). So, in many cases I would prefer to just do a loop than list comprehension, as a lot of list comprehensions are just to hard to read once you cram a lot of chaining and logic into them.

If you are chasing microseconds in Python, then honestly you are using Python wrong. Might be time to switch to other language.

movaxh
Автор

Hey man, here before your channel blows up. Great short videos explaining some very important topics in Python. Being a Python developer myself, have started using these tricks/methods for implementing more efficient codes. Great Job!!!

maroofkhatib
Автор

you convence me with the timeit test cause i love list comprehension.

miguelvasquez
Автор

JS's (x)=> and haskell's \x ->
are just so much nicer than python's 'lambda x:'. Python's named function declaration is 'def' which is shorter anyway.

ezg
Автор

I'm interested to know if using the lambda was the issue with the map function speed or that the lambda was calling another function altogether.. would have been better to show the speed of using the lambda to do the code that process_text is doing without calling it.

shannonblack
Автор

You deserve a lot more views
Dunno what's wrong with YT algo ☹️

FauziNomad
Автор

In my experience, the map keyword approach is less readable most of the time, I like to use comprehensions instead even though it might be slightly slower.

Lanxxe
Автор

What's this code => people: list[str] ? is it new type in a version of python?, I've got an error with this line within python 3.8.10

Asgallu
Автор

Nice video ! Thanks, i was always using the `lambda x: func(x)` syntax i will start replacing it from now !

hugoavila
Автор

what is the reason though? why is map 25% faster than list comprehension for the example u showed? Can you speak to that please?

adityahpatel
Автор

I haven't seen this syntax before, how are you getting that?

christopherwiseman
Автор

If you both like the comprehension syntax (I could accept the map syntax as being cleaner but the comprehension syntax being more Pythonic) & you also like the efficiencies & laziness of a generator, couldn't you instead just use the generator comprehension syntax?

cosmicaug
Автор

people : list[str]=["abc", "def"]
VS
people =["abc", "def"]

1. Is there any difference between these lines?
2. Why should I use the 1st method to declare a list over the 2nd
3. Is the 1st declaration method holds any specific name??

Please say, I am a beginner

mainakmukherjee
Автор

What about generators? Are maps as memory efficient as generators?

JorgeLuis-tsqp
Автор

What is mean people : list[str] ?
Please answer me!

parthiban.m
Автор

I just realised that the Python logo has the colours of the Ukrainian flag.

Phlypour