Python Tutorial: List Comprehensions Step-By-Step

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

This video tutorial breaks down Python list comprehensions for you step by step. See how Python's comprehensions can be transformed from and to equivalent "for"-loops so you'll know exactly what's going on behind the scenes.

One of my favorite features in Python are list comprehensions. They can seem a bit arcane at first but when you break them down they are actually a very simple construct.

The key to understanding Python's list comprehensions is that they’re just for-loops over a collection expressed in a more terse and compact syntax.

In the video I also discuss when to use list comprehensions in your own Python programs and when not to. Basically, the caveat to Python’s comprehensions is this—as you get more proficient at using them it becomes easier and easier to write code that’s difficult to read. If you’re not careful you might have to deal with monstrous list, set, dict comprehensions soon. Too much of a good thing is usually a bad thing.

* * *

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

I'd say out of all list comprehension tutorials i looked thru, this one presents that matter in most straight-forward way.

major_desync
Автор

Wow, I've spent a whole week on YouTube and paid resources and this video explains everything I needed to know about iterating through a list.

mizurashi
Автор

I struggled with this concept previously but this video cleared it up, Thanks!

imxron
Автор

programming needs more of you!
well done you pin-point the exact confusion because most of the videos explain how to make a list comprehension but forget to explain the main components!

educationaltechnology
Автор

This tutorial really helped me understand list comprehensions. I watched 4 videos and this is the best one by far. Thank you so much!

lorisegovia
Автор

this is by far the best explanation for list comprehension and filtering out there, even the paid videos won't stand a chance. Precisely explained!

mailrafat
Автор

I think list comprehensions would look familiar to someone with an SQL database background since they follow the same pattern as an SQL SELECT statement:

SELECT expression FROM table WHERE condition

MarcPoulin
Автор

Thanks a million for the template and breaking it down the way you did!! IDK why other tutorial vids skip this. If they took a moment and did what u did the world would be a better place 👍

androspuddy
Автор

Using a phenomenal app on android to ramp up, but some areas are glossed over a bit too much.
This was the perfect place to come and the perfect teacher to help me me get it. thanks man!

mattgraves
Автор

writing out the forloop equivilance of the code really helps

GrandNecro
Автор

I really like the way you used mutli-lines in the list comprehension. It looks more readable to me. Ill probably start writing them that way. Thanks for the videos.

marcd
Автор

Don't forget you can use an ELSE in list comprehension too. Just learned that there are ELSE's in For loops and Try's

AgentJRock
Автор

I struggled for week to understand . But it cleared in just 14 min . Superb...

shyambiradar
Автор

way you explained the concept using template was brilliant!

abhideepsingh
Автор

This was definitely one of the best videos on the topic; thank you.

arsbadmojo
Автор

Very straightforward and clear explanation. Thanks.

charliegreenaway
Автор

Really helpful video, with some great tips thrown in!

Raggaliamous
Автор

If you want to use an if-else logic in a list comprehension, the tamplate is:
[expression1 if condition1 else expression2 for value in collection]
e.g.
[int(x ** (1/2)) if x ** (1/2) % 1 == 0 else x * x for x in range(20)]

BtsRifer
Автор

I love the listcomprehension syntax but I tested them vs funcs like map(), filter() and funcs are faster

CentboyMidnight
Автор

wow thank Man, you make this concept easy to understand

BagusSulistyo