10 Python Comprehensions You SHOULD Be Using

111,439
0
Published 2024-03-29
In this video, I will be showing you 10 Python comprehensions you can & should be using. These comprehensions will progress in difficulty, so be sure to stick around until the end of the video!

If you want to land a developer job: techwithtim.net/dev

⏳ Timestamps ⏳
00:00 | Overview
00:11 | Basic List Comprehensions
02:06 | Comprehension Condition
04:22 | If You're Serious About Becoming A Developer
05:20 | Comprehension With Multiple Conditions
07:42 | Multiple List Comprehension
09:18 | If/Else In A Comprehension
11:22 | Nested List Comprehension
14:32 | Transformation In Comprehension
15:38 | Dictionary Comprehension
16:47 | Set Comprehension
17:49 | Generator Comprehension

Hashtags
#techwithtim
#programming
#python

All Comments (21)
  • @kerrykreiter445
    Absolutely the best tutorial I’ve watched on comprehensions. Thank you for making it very understandable. I would also appreciate the same type of video covering Lambda functions. Thanks again for helping so many!!
  • @Indently
    07:28 I actually never knew you could insert multiple conditions like that, pretty cool!
  • @whiteedward1
    I've barely learned anything in python and easily picked up the information. So simple, yet elegant explanations. Thank you. It has already helped me in my assignments.
  • @mikespon
    I love your teaching style. Very easy to follow. Thank you for all your hard work.
  • @BiologyIsHot
    The syntax for flattening a list always feels very wrong. Given that single list comprehensions put their iterable to the left, it always feels odd that for num in row is to the right of matrix. It REALLY feels like it should have been [num for num in row for row in matrix] instead of what it actually is. I actually think this is one of the worst bits of Python and I encourage people to not do these neste/unpacking list comphrensions because of how unreadable they are. It would have been great if they gave us some kind of unpacking operator syntactic sugar here with [*row for row in matrix]. That would have been a much better syntax than [item for sublist in nested_list for item in sublist]
  • @BruceDuncan
    Oof you got me. Been writing python for 15 years and never knew that you could have multiple `if` statements in a comprehension. I have always written `and` and assumed you made a syntax error 😂
  • @swagatopablo
    Most comprehensions can be replaced by some combination of map and filter, which I find far cleaner. Further, in most cases, you will iterate through the iterator only once, in which case, you can leave it as an iterator (map and filter) form until you use it (unlike, for example, using comprehension to give you an iterable). This is far more pythonic and memory efficient too.
  • @mikec64
    I found the way you formatted the comprehension that calculates squares is a really helpful way to think about comprehensions, even if I compress them to a single line. In that form, it looks like the comprehension just skips the result=[ ] statement, which I always thought was ugly. And in nested loops it gets rid of all those intermediate lists and variables. For the first time they look more elegant to me.
  • @Soyosan22
    This is an absolutely excellent video. Your examples and explanations are always great. I think it is time for you to start working on a Python book. I am sure it will be a huge success. :)
  • @kapibara2440
    Great content, like always from you. Thanks Tim! Greetings from Poland 😊
  • @lucasseagull8282
    Man, you grew a lot in past 3 years :)) I'm just watching your video called: Python Selenium Tutorial #2 - Locating Elements From HTML, where you are referring to your website for testing. However, it's no use as the website is now different - can you do the remake of your selenium tutorials / make it up-to date? Thank you and good luck!
  • @andresbonelli
    You can even nest comprehensions inside of other functions, inside of user inputs... ie: print(*(list("Hello" for _ in range(int(input("How many 'Hello' would you like to print? ")))))) Python is wild...
  • @RadChromeDude
    Man list comprehensions are just amazing. I've just finished a level 1000 coding module in python. Basically, i ended up (ab)using list compre wherever I could. It's just that elegant!
  • Looks good. I'm just now learning it but as far as I can tell it looks way better than the original code.