Do you even need loops in Python? A Python short by Michael Kennedy

preview_player
Показать описание
Have you heard you shouldn't write loops in Python? Don't you *need* loops for programming? Yes, we do! But Python has special, concise language constructs that model loop-like behavior that often are more appropriate. In this Python short, you'll learn the hows and whys of converting for-in loops to list comprehensions in just a few minutes.

Enjoy this Python short "quick hit" video by Michael Kennedy, a Python Software Fellow and host of the Talk Python To Me podcast.

▬ CODE ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

▬ Chapters ▬▬▬▬▬▬▬▬▬▬▬▬

0:00 - Introduction
0:48 - Explore the existing app with car sales data
1:55 - Running our for-in loop code
2:28 - Language format of the list comprehension
3:24 - Converting the for-in loop to the list comprehension
4:32 - Conclusion

▬ More Videos ▬▬▬▬▬▬▬▬▬▬▬▬

▬ Dive deeper ▬▬▬▬▬▬▬▬▬▬▬▬

Рекомендации по теме
Комментарии
Автор

Always loved List Comprehensions. Much cleaner and succinct.

Colstonewall
Автор

Note that there is a for loop inside the list comprehension... So yes, you do need loops in Python.

jabuci
Автор

You still have the for loop in list comprehension

buzzdev
Автор

I do like list comprehensions, but tend to write them in three lines if i got the conditional statement :D

Minkiu
Автор

Have an idea for this short series. How about an async version of this short? maybe going as far as async generators or async generator expressions (if there is such a thing)

Автор

Great video Michael. One comment could be a follow-up video that explains using Generators for "Larger-than-your-PCs-Memory" datasets. Using the same data, for clarity.

spouwn
Автор

Do you even need loops in Python? - Yes
that you are using in list comprehension - Violla Genius LOL

HimanshuYadav
Автор

I've read this before, but generally for readability, I have kept with for-in loops. What is the performance gain by doing a list comprehension? Is this more focused on massive data sets? Another great video...thank you Michael.

galanu
Автор

Does list comprehension work with updating additional variables in a standard for loop? For example, I have a bit of code nested in a while loop that will check if a file exists, then update the version name to the next incremental value if true, and compile the file results into a list. I would be blown away if list comprehension is that robust but I don't know enough about it admittedly. I will update my code immediately if that's possible!

Edit: found a post that specifies that while loops are not compatible with list comprehension. Ah well, it would be nice.

Drakkle
Автор

I love vectors, they're my friends.

KambizTavabi
Автор

Is there a reason to not get_raw_data() in your comprehension?

iNuchalHead
Автор

Still same. can you show how they compile?

guidyouguy
Автор

Thanks Michael! Love your work! By the way, why do you annotate with list[Something], not List[Something] (from typing module)? More simple? Is there any difference?

victorklimov
Автор

I find it misleading to say a list comprehension helps to build better or more reusable code. It does not help with reusability. Its just what it is, a way to describe the generation of a list.
The "you dont need loops" approach made some people come up with weird things like using list comprehensions for thing that dont require the generated list, or dictionary or set, and do that as a syntax sugar to for loops.

AbstractObserver
Автор

I like list comprehension.. But there is a for look written right there in the middle of the comprehension... So saying you are avoiding a for loop is disingenuous

DuarteMolha
Автор

You should add more lighting, you're face isn't completely overexposed and washed out yet. Ghost with glasses 🤓

StupidInternetPeople
visit shbcf.ru