Python Tutorial: Comprehensions – How they work and why you should be using them

///Python Tutorial: Comprehensions – How they work and why you should be using them

Python Tutorial: Comprehensions – How they work and why you should be using them

FavoriteLoadingAdd to favorites

Python comprehensions are a very natural and easy way to create lists, dicts, and sets. They are also a great alternative to using maps and filters within python. If you are using maps, filters, or for loops to create your lists, then most likely you could and should be using comprehensions instead.

In this video, we will look at how comprehensions work, why you should be using them, and the benefits they have over the alternatives.

The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/List_Comp

If you enjoy these videos and would like to support my channel, I would greatly appreciate any assistance through my Patreon account:
https://www.patreon.com/coreyms

Or a one-time contribution through PayPal:
https://goo.gl/649HFY

If you would like to see additional ways in which you can support the channel, you can check out my support page:

You can find me on:
My website – http://coreyms.com/
Facebook – https://www.facebook.com/CoreyMSchafer
Twitter – https://twitter.com/CoreyMSchafer
Google Plus – https://plus.google.com/+CoreySchafer44/posts
Tumblr – https://www.tumblr.com/blog/mycms

By |2017-05-01T12:21:32+00:00April 26th, 2017|Programming Video Tutorials, Python Video Tutorials|50 Comments

50 Comments

  1. Mojo Tech April 26, 2017 at 9:18 pm - Reply

    excellent tutorial! I just started using python at work and my mentor showed me this the other day.

  2. Jdms 8911 April 26, 2017 at 9:18 pm - Reply

    i love u

  3. lokesh bhatt April 26, 2017 at 9:20 pm - Reply

    Great, fast paced contents with nice illustrations !

  4. NeuroKernel April 26, 2017 at 9:21 pm - Reply

    Very very Nice comprehension examples

  5. No One Is Listening Until I Fart !!! April 26, 2017 at 9:21 pm - Reply

    You should be a teacher. You speak steadily without farts and explain very detailed.

  6. insanecbrotha April 26, 2017 at 9:23 pm - Reply

    the dict comprehension works also with something like this (this was my first idea when I read the verbose task description):
    :
    fullname_dict = {names[i]: heros[i] for i in range(len(names))}

    Unlike the given solution this throws an IndexError if the iterables don’t have the same len though.. 🙁

  7. Robert M. April 26, 2017 at 9:25 pm - Reply

    Great videos! How about a tutorial for map and lambda? I’ve worked through a couple beginner Python books and haven’t heard of these.

  8. W. Schobel April 26, 2017 at 9:26 pm - Reply

    your videos are brilliant! glad i found your channel

  9. Jeffrey Stockdale April 26, 2017 at 9:28 pm - Reply

    1st time seen this explained, I agree, your lack of "ummms, uhs, hold-ons, etc." are very pleasing to the ear, great instructor. Thanks.

  10. Carl Gusler April 26, 2017 at 9:28 pm - Reply

    Great and helpful video. Which IDE is this?

  11. jerril42 April 26, 2017 at 9:31 pm - Reply

    I’ll never look at lists the same again 🙂

  12. Probuddha Nag April 26, 2017 at 9:31 pm - Reply

    I am learning Python and your videos have been more than helpful. Thank you very much!

  13. Chris Baconham April 26, 2017 at 9:32 pm - Reply

    for the dict/hash comprehension, imo using dict function is shorter and easier. especially in python 3 since zip returns its own "zip type". although, you still can unpack returns with the same way, but type less is alway good, right?

  14. Augusto Vera April 26, 2017 at 9:33 pm - Reply

    Somehow my cortex was aroused when the comprehension parts started.

  15. themossinator April 26, 2017 at 9:36 pm - Reply

    Hi, what program are you using here to run your code? I love the dark background, great vid, thanks

  16. David McLaughlin April 26, 2017 at 9:36 pm - Reply

    Fantastic tutorial. Thank you.

  17. Cool Guy April 26, 2017 at 9:37 pm - Reply

    Thanks for your videos on python. I need tutorial on the datetime, time and calendar modules on python with easy understandable examples from the scratch on those modules. Can you help me with that?

  18. Gyanender Singhle April 26, 2017 at 9:37 pm - Reply

    Can you please make some tutorials on python3 and selenium framework….

  19. Gian Bisma April 26, 2017 at 9:41 pm - Reply

    Question: Is there a certain code that can check if all numbers in a list are identical?

    p.s. Your videos really help me go forward with coding!

  20. Leith Hawkins April 26, 2017 at 9:44 pm - Reply

    Awesome Tuts. Well thought through but clear and concise ! well done

  21. fooser April 26, 2017 at 9:44 pm - Reply

    how do you remember which types of brackets to use for each example? The obvious ones are [ ] for list, { } for dict, ( ) for sets.

  22. Raviteja Bhavirisetty April 26, 2017 at 9:47 pm - Reply

    Very helpful video. 🙂
    One question though! To remove the duplicates in an array we can simply use the set function right?
    my_set = set(array)

  23. sharad chandakacherla April 26, 2017 at 9:48 pm - Reply

    thanks man.

  24. André Eriksen April 26, 2017 at 9:48 pm - Reply

    heroes*, good vid

  25. Bob Jasinski April 26, 2017 at 9:50 pm - Reply

    I have a question about zip . In case of print(zip(names,heros)) I received
    <zip object at 0x00000172F53A37C8>
    I wonder why you received a tuple as on output.

  26. Pantelis Karamolegkos April 26, 2017 at 9:50 pm - Reply

    Perhaps an alternative to list with my_nums if even:

    my_list = [n for n in nums if not n%2]

  27. Srineesh Salur April 26, 2017 at 9:51 pm - Reply

    i am getting output as <filter object at 0x00D25AF0>
    when i use lambda functions pls help
    code:
    nums = [1,2,3,4,5,56,70]
    king = filter(lambda o:o%2 ==0,nums) i am using pycharm

  28. Anuradha Pani April 26, 2017 at 9:51 pm - Reply

    very nicely explained! thanks

  29. Ramesh K April 26, 2017 at 9:52 pm - Reply

    Excellent

  30. hslilly April 26, 2017 at 9:55 pm - Reply

    Thanks for the videos. Just subbed.
    n.b.: Python3 zip returns an iterator, I needed to create a list then print.

  31. darrenandveronica April 26, 2017 at 9:56 pm - Reply

    Succinct yet comprehensive, and beautifully explained. I am really getting a lot clearer about python through your videos – thank you.

  32. Jose Lima April 26, 2017 at 9:59 pm - Reply

    finally, you have glued this together in my head. thanks!

  33. Entertainment ke Baap April 26, 2017 at 10:00 pm - Reply

    plzz add a tutorial for abstraction in Python… asap

  34. Mark ass trick April 26, 2017 at 10:00 pm - Reply

    awesome video! I didn’t know about sets and generator compressions. will be trying them out. thanks!

  35. Lesh Ghett April 26, 2017 at 10:01 pm - Reply

    Just wanted to know how you put comment symbols on multiple lines at once, thanks.

  36. Bob Box Body April 26, 2017 at 10:01 pm - Reply

    nice one, should do some huge random stuff both ways and cmp execution times. Did you thumbs down your own videos just cause?

  37. Sunil Kumar April 26, 2017 at 10:02 pm - Reply

    Nicely explained. well done. and Thanks.

  38. Lenny Atomz April 26, 2017 at 10:03 pm - Reply

    Fantastic video! Thank you for sharing your knowledge with us, Corey! 🙂

  39. junar tagulao April 26, 2017 at 10:03 pm - Reply

    This is the best python tutorials I’ve ever seen so far. Clear, very detailed, even the examples and text editor you’ve use is perfect. thanks, looking forward for more advance python techniques in the future. Once again.. thank you so much

  40. battlemode April 26, 2017 at 10:04 pm - Reply

    Really good tutorial, very clearly and concisely explained. Thank you!

  41. Enock Kwesi Addey April 26, 2017 at 10:04 pm - Reply

    Thanks so much! Great examples.Very helpful!

  42. Gyanender Singhle April 26, 2017 at 10:05 pm - Reply

    your tutorials are really helpful and best available on you tube they are very detailed I have gone through multiple tutorials on Python for ex Sentdex ,newboston .but none is as detailed as your you have gone from basic to advance …….thanks man ….

  43. Huy Nguyen April 26, 2017 at 10:05 pm - Reply

    That was really good. Thank you very much.

  44. Matias Daneri April 26, 2017 at 10:06 pm - Reply

    Very good videos!

  45. Maziar Ghorbani April 26, 2017 at 10:06 pm - Reply

    You are a talented tutor. Thanks

  46. Bill H'Oderow April 26, 2017 at 10:06 pm - Reply

    excellent

  47. ISA Aslan April 26, 2017 at 10:08 pm - Reply

    hello can you show how to change from for loop in to list comprehensions

  48. Sanjay Shr April 26, 2017 at 10:08 pm - Reply

    how do i create multiplication table using LC(List Comprehension) ?

  49. Saradee113 April 26, 2017 at 10:10 pm - Reply

    YES! thank you so so much! All other tutorials kind of just mention this casually thinking that we understand what is going on, but we cant really know what’s happening until someone actually explains it! You are an amazing teacher

  50. 42 April 26, 2017 at 10:12 pm - Reply

    Thank you.

Leave A Comment

*