Python Tutorial: List Comprehensions Step-By-Step

///Python Tutorial: List Comprehensions Step-By-Step

Python Tutorial: List Comprehensions Step-By-Step

FavoriteLoadingAdd to favorites

► See how to leverage Python’s best features, like List Comprehensions, in your own programs

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.

To deepen your learning also check out this longer tutorial article I wrote on Python’s comprehensions, including dictionary comprehensions and set comprehensions:

FREE COURSE – “5 Thoughts on Mastering Python”

SUBSCRIBE TO THIS CHANNEL:

* * *

► Python Developer MUGS, T-SHIRTS & MORE:

FREE Python Tutorials & News:
» Python Tutorials:
» Python News on Twitter:
» Weekly Tips for Pythonistas:
» Subscribe to this channel:

source

By |2020-05-02T03:22:41+00:00May 2nd, 2020|Python Video Tutorials|24 Comments

24 Comments

  1. Rafat Mohammed May 2, 2020 at 3:22 am - Reply

    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!

  2. lylez00 May 2, 2020 at 3:22 am - Reply

    My opinion is that we should only use small list comprehensions. They're not going to be very maintainable otherwise.

  3. UR982T May 2, 2020 at 3:22 am - Reply

    This reminds me of lambda functions I was reading about earlier! I’ve also seen map() used and people say it’s not the “pythonic” way. What does that mean?

  4. SUCHANDRA GHOSAL May 2, 2020 at 3:22 am - Reply

    why use list comprehensions instead of map and filter , which is faster?

  5. D A May 2, 2020 at 3:22 am - Reply

    Thanks Dan – excellent video.

  6. Hnin Yu Hlaing May 2, 2020 at 3:22 am - Reply

    I really thanks to you 'cause I'm clear with that viedo

  7. Alphabet May 2, 2020 at 3:22 am - Reply

    <>

  8. Matt Graves May 2, 2020 at 3:22 am - Reply

    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!

  9. Philip Hackman May 2, 2020 at 3:22 am - Reply

    How to Loop through a list of names, for each name lookup their age in a dictionary and print the name, age?

  10. aayush bajaj May 2, 2020 at 3:22 am - Reply

    Thank you soo much i was finding the list comprehension tough u have just started learning python and your vedios are a great help
    And one tinny question which compiler do you recommend…? (For beginners)

  11. GrandNecro May 2, 2020 at 3:22 am - Reply

    writing out the forloop equivilance of the code really helps

  12. innershapes May 2, 2020 at 3:22 am - Reply

    what x * x stands for? are they previously set variables?

  13. GAPrivaldo May 2, 2020 at 3:22 am - Reply

    Nice and clean, thumbs up

  14. Khalid Mohammad May 2, 2020 at 3:22 am - Reply

    شكرا دان بدر

  15. Ragnar Lothbrok May 2, 2020 at 3:22 am - Reply

    Lately I've tried to use this with two arguments but Im failing : [i,x for i,x in enumerate(my_items)]. Can this be done somehow?

  16. Avit Info May 2, 2020 at 3:22 am - Reply

    why there are 5 dislikes ?

  17. Jeff Hunter May 2, 2020 at 3:22 am - Reply

    I've been programming for over 15 years but I'm still really noob and get laughed out of every job interview, do you think you can train me over skype? I'm really tired of working in grocery stores.

  18. Abhideep Singh May 2, 2020 at 3:22 am - Reply

    way you explained the concept using template was brilliant!

  19. Marek Oko May 2, 2020 at 3:22 am - Reply

    Thanks, your video helped me to figure it out! 🙂

  20. Bagus Sulistyo May 2, 2020 at 3:22 am - Reply

    wow thank Man, you make this concept easy to understand

  21. Cezary Barański May 2, 2020 at 3:22 am - Reply

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

  22. Mylovelyform May 2, 2020 at 3:22 am - Reply

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

  23. Shyam Biradar May 2, 2020 at 3:22 am - Reply

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

  24. arsbadmojo May 2, 2020 at 3:22 am - Reply

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

Leave A Comment

*