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



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:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

Nguồn: https://benjaminjcohen.com/

Xem thêm bài viết khác: https://benjaminjcohen.com/cong-nghe/

31 Comments

  1. 4 years later still the best tutorial available thank you so much, sir.

  2. Its so wonderful na the course which should be a paid one is available for free

  3. it says " append() takes exactly one argument (2 given) " what should i do? ( i am using an IDE called Spyder) . I tried the code in another python IDE and it works. I am confused why this is happening? Please help me

  4. But isn't he creating a tupple in the last example? what made it a generator?

  5. Thanks for this video…Jajakalla Kharian…

  6. I've learnt a lot from your videos. Thanks heaps.
    I have a question though.
    When I tried to print(zip(names,heros)), the result I got was zip object at….(I'm using Google Colab to practice alongside with your vids)

    Anyone can help me on this one, I'll very much appreciate it.
    Thanks x

  7. Great video, thank you! 11:03 is your print zip(names, heros) a configuration? I was only able to get print(zip(names,heros)) to return <zip object at {memory_loc_id}>

  8. Sir, can you make a detailed video on maps and lambda function too?

  9. Thank you for this great tutorial!

  10. I couldn't use print function without (). How can he use like this? 3.7 anaconda btw

  11. i cant get it at 15:29 if you used {} then why output is in [ ] and in ( ) why curly{ }?????

  12. i think this comment may help some one if we want to create a list of even then
    evenlist=[x for x in range(10) if not x%2]

    i am watching the video while coding.
    i am new to python and its interesting language.

  13. if we want to pass more than one params to the lambda function the
    map(lambda n,x: n*x,range(4),range(4,8,1))

    we can do in this way.

  14. Thanks for the video! I have two questions: 1) Why don't you put parenthesis before print? It doesn't work for me this way and I always need to put print(). 2) It looks like zip works for you as print zip (names, heros). If I do that (with parenthesis after print), it just prints the location in memory. I have to use print(list(zip(names, heros))) in order to get the pairs. How come you don't have to use list in your example?

  15. Can't you use dict function instead of dictionary comprehension?

  16. Could hardly be better. Great hands-on teaching!

  17. is there any way of using list comprehensions like N = 4 now i want a list like arr = int(input()) –> for 4 time at a time separated by space in a line? Sorry for my bad english.
    i can for arr = [int(x) for x in input().split())] but that doesn't use the N value to check ? Is there any way to get fixed input with list comprehensions . TIA

  18. which py version are you using ? py3 doesnt support print statements as you show here.., right ? Am I correct ? Are you using py2 ?
    If you are using py3, can you please explain how to have this feature enabled for print method…?

  19. thers a slight tint of southern accent in your English

  20. Best Python tutorials in youtube!

  21. What is the meaning of 'comprehension' in these operations? That is the most puzzling point for me personally. Thanks.

  22. 'check out 11:21' when i tried to print (zip(names,heros)) it throws output <zip at 0x10aa9bf00> instead of printing . i am using python 3 in Anaconda env.?

  23. Can pickle be used in list comprehensions loop?

  24. Do you have a tutorial on python script to scrape data out of tables embedded in PowerPoint files?

  25. CONCISE and ELOQUENT are the words for you. First content youtube channel that I don't have to speed up the videos. Thanks!!!

  26. does it work for string also

  27. why is it that in the video print my_list is working but in my system, it is required to use () around my_list like print(my_list)

  28. Corey I've been following your tutorials for quite a couple of months and what you teach is clearly unique than what other tutors are teaching on youtube. I mean the concepts and topics you choose. would you plz suggest me some books for learning python and django more in depth ? I hope to get a reply soon.

Leave a Reply

Your email address will not be published. Required fields are marked *