Python Tutorial: Comprehensions - How they work and why you should be using them
Вставка
- Опубліковано 8 вер 2024
- 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:
github.com/Cor...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com...
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python
8 years after and my man is still better than 90% of other tutorial, cheers man
9 years**
Corey I hope you always go to bed feeling good about your work and helping out the community. Thanks for your very clear tutorials.
Positivity ❤️❤️
I bet he would feel and sleep even better if you supported him.
I have to say that your videos are among the best that I've found on youtube for learning python.
You're very well spoken, and your examples are great at illustrating the concepts!
+Crow Thanks for the kind words. I appreciate it!
+Crow I agree with this. He really knows how to explain things.
5 years
I guess im asking the wrong place but does anyone know of a tool to log back into an Instagram account??
I was dumb forgot my login password. I love any help you can give me
1st time seen this explained, I agree, your lack of "ummms, uhs, hold-ons, etc." are very pleasing to the ear, great instructor. Thanks.
Corey, you have really helped me to make the leap from beginner to intermediate Python programmer. I'd heard of list comprehensions before, but never dictionary comprehensions. Once again, many many thanks!
yes so true. I came to learn list comprehensions now going back with even more comprehensions :)))
To be frank,this is the best video for learning about list comprehension on UA-cam!
This is an exceptional tutorial on list comprehensions. The use of writing out what we want in normal language, seeing it implemented in a loop, and then how it translates to a list comprehension makes it very clear and intuitive. I never quite got comfortable with them when I originally learnt this, but your method of teaching it allowed me to understand the concept. Many thanks.
the amount of material I have learned from this channel is literally amazing!
I just started learning Python and decided to rewrite a program in Python that I first wrote in C++. I wanted to use a lot of these Python features to eliminate all those those for-loops. So far, my Python program is turning out to be a lot more complex than I thought it would be. (I think I'm over-thinking the problem.) I'm hoping I can use list comprehension to eliminate a lot of that coding and fully utilize the power of Python. I happened to find your video, and I know it is really going to help me. I was also looking for a way to code, debug, and run .py files. You are doing things in Sublime Text that I didn't know you could do. I'm looking forward to watching the video again and following along, as well as watching more of your videos.
You just saved me on my homework assignment. I looked at several resources online and asked three different people and your video is the only thing that made the light bulb in my head turn on (in learning dictionary comprehensions)!! Thank you.
CONCISE and ELOQUENT are the words for you. First content youtube channel that I don't have to speed up the videos. Thanks!!!
Corey, I never leave any comments under YT videos but man, your tutorials are just something else. Cannot THANK YOU enough for being such an amazing teacher. I love everything about them: the way you explain concepts, carefully go through examples over and over again...and you just sound like a cool guy!
Thanks for everything Corey, truly appreciate you are here for us.
I got confused with your print function in this video, i.e print my_list instead of print(my_list). After a little research I discovered the former is python 2.x while the latter is python 3.x.
I also discovered I can directly create a dict with the zip keyword i.e dict(zip(names, heros)).
Finally, I love your videos, they are straigtforward and refreshing.
hey man, same here, why? already solve?
This was the most comprehensive video I've seen on comprehension.
A person could be a good programmer but good programmer != good teacher. You are the best python teacher on UA-cam.
Thanks for making our lives easier by putting the hard work in making those videos and explaining the concepts in simple and understandable form.
Also, whenever you have time, please consider making video on decorators.
well, he still is a good teacher!
@@codingsafari He just told him he's the worst python programer on UA-cam lol. Could also be interpreted as good programmer != good teacher, but you can't say best programmer != best teacher, as otherwise programmer != teacher would be correct.
at 12:00 you are able to create dict from zip:
dict(zip(names, heros))
Yes, that is a way of doing it, but his method is more pythonic.
is that python2 only or for both python2 and python3?
@@liangjiang3122 Both, but you might get unexpected result, like the one in a commend above.
I'm so mad at the viewers for not subscribing.. Mate you DESERVE a million subscribers.
Just spent a couple hours reading articles, watching videos, and practicing nested comprehensions. The whole time I was thinking "wow if Corey had a video on this it would have been so much faster and easier to understand!" Your channel is awesome :)
I suddenly remember my best Algebra 👨🏫 because of you! - Thanks Corey, I wonder where you are right now,
want to shake hands with you and meet you!, You are a great teacher. 👨🏫
oh mannnnnn....... simply AWESOME.. You teach way better than most of the python tutors. Your detailed explainations are dope.
I know Im immature but every time someone calls a dictionnary variable : my_dict I burst out laughing
Ahahahahahahahahahahahahahahahahha so funny.
12:28
LOLZ 😂😂😂😂😂😂🤣🤣🤣🤣🤣🤣🤣🤣🤣😆😆
Help I cant find the unsee button!
You scarred me for life. I did not have the dirty mind but I have to use some other variable name to keep my sober face while I am teaching.
I watched every topic video before the topic my class is going to reach. It makes my class so easy.
Nice tutorial. Please emphasize, if you use two for loops within list comprehension, the second one is the Nested loop. You can use different condition in each for what you want. List comprehension can also create not only list of variable, but the list comprehension: [[i for i in something] for j in something] --> list with nested lists inside.
Glad I found your channel.
I am working my way through Automate the Boring Stuff with Python and stopping after every chapter and watching your videos has helped so much in understanding the topics in greater detail.
Your python series conquer the whole youtube, the explanation is concise and to the point. I especially enjoy those real word examples, Hope you will make a series to feature more and more real world examples. Thank you.
Thank you Corey, content made 7 years ago and still fresh.
04:15
python 3.6 should this:
my_list = list(map(lambda n: n * n, nums))
Yes, and even for filter() as well.
and list(zip())
Thanks
@Nasa Lee Thankyou
thanks bro.... Was stuck in that....
Boy you're a good teacher! Great explanation followed by meaningful demonstration. Clear.Punchy. Keep going I'll keep watching.
Wow, a concise video teaching elegantly creating lists, dictionaries, sets, and generators!
All I can say is amazing contents never die...They still live for decades to come
An elegant approach of illustration, Beautiful!!!
Best series of videos on Python on the internet.
OMG you made these concepts to simple it took me forever to understand generators till I found your video.
I was trying to learn that for over a year but never got that the exact format or remember it. But you saved me. SO WELL EXPLAINED .merry christmas
I hope you never take down these videos , so convenient to always be able to come back to them again when not understanding at first and then real life situation makes one have to u understand and then these videos are so helpful THANK YOU best python learning channel I ever used
Wow! Now I finally understand list comprehensions. Thanks Corey. Another great tutorial.
Thanks. I had not heard of set comprehension or generator creation using parens, and I appreciated the excellent refresher on other topics!
You are the best tutor for me. I understand ur style than anyone. Thanks
Using these videos to fill in some of the gaps I've had while reading "Automate the Boring Stuff". Been making cheat sheets to code along with off your videos!
4 years later still the best tutorial available thank you so much, sir.
I've taken several paid courses on the Internet - I won't mention the company..... I'm in the middle of one now on Plotly and Dash. They are mostly okay, but even the best of them is not as good as your videos. Professionalism, pace, production, the way you progress through the stages of solving a problem or teaching so that it is easy to follow you through the steps, fluid, no wasted time or confusion in correcting errors, even voice, which is important. One thing that site asks you to rate is how engaging the teacher is. There was only one that was even in your class.
I'm sure you do well in your day job, but it's amazing to me that with this teaching skill you have, you didn't go in that direction professionally.
This is by far the best tutorials on list comprehension I’ve ever got online. Thank you so much!
I am very grateful that you make these videos, keep up the great work!
Must say your videos are best in class and I have learnt so much from them. Your explanations are very clear and makes even tricky concepts easy to grasp.
Unfortunately it is not possible give you more than 1 like. You are the best man! Thanks Corey.!
Brilliantly done. You were very well prepared and it showed. Bravo sir.
Finally see an ad in this tutorial video! Chosen not to skip the ad so Corey gets the pay from UA-cam! This is the least I can do to express my gratitude for the great teacher!
I think you are very good at Python and the way you present things; clear & to the point. You are among the best out there. God bless you!
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
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
For those on python 3.6 at 11:20.
result = zip(names, heros)
result_set = set(result)
print(result_set)
This may be an individual thing where mine didn't work using the method corey used, but this seemed to work fine.
The best explanation ever! You're making our lives so much easier! Thanks!
This wasn't what i was looking for, BUT i am very glad i stumbled upon this video! Thank you!
Thank you bro. Your lectures are GOLD!
Very useful even after 6 years, thank you!
Explained wonderfully. Thank you.
Best Python tutorials in youtube!
Awesome, I have watched all of your python videos and they are great stuff , Thank you
I like your vids, they are well explained. It helped me to get two things straight and use list comprehensions:
The first n in for n in nums is the same as my_list.append(n)
The one-liner has the same built as a for loop. First for n in nums and then the if.
best python videos on youtube..
Very good video to understand list comprehensions
Hey Corey, could you do a video on lambda, map, reduce and those family of lovelies?
+SeekHeart Sure thing. Just wrote that down. I'll try to make a video on those in the near future.
Thanks man!
+1
yes please make a video over that
we love to get immediately level when we finish this course. Thanks so much for your courses.
You are a great teacher. Keep it up! Thanks
Man, you're the real MVP. Thanks a bunch.
Succinct yet comprehensive, and beautifully explained. I am really getting a lot clearer about python through your videos - thank you.
Thank you Corey for your time and effort. Keep spreading the knowledge
ABSOLUTELY AMAZING EXPLANATIONS!
Great video! The concept of list and dictionary comprehensions was very well and clearly explained. I understand it much better now.
I just want to add that the generator expression at the end can all just be written in one line I believe: print(list(n*n for n in nums)) though that would only print it as a list rather than line by line.
Great explanation! "[item for sublist in list for item in sublist]" Is it same for what you have shown in 9:18 min about nested loops but in your example it is for dictionary comprehension. [item for sublist in list for item in sublist] is used for flattening nested lists, that's where I took it from
This was very informative. Your examples helped a great deal.
I saw a dictionary compression for the first time today and was super amazed I had never encountered it. Eventually led to this video!
Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.
i normally don't post comment .. But could not help on this one. Very well laid out .. nicely explained and very well planned ... good job thanks
Thanks!
Best video on lists.
Fantastic teaching!
Whenever fall problems in programming I searched that problem in your channel.
You are the best on youtube, God thank you.
learned a lot from this best video ever seen thank u
Thank you. Even though i didn't come for list comprehension this solved my lack of knowledge with lists in python! Definitely will look more into your channel once i need to seek more knowledge.
Dude you are a rockstar! Thank you for making this easy to follow tutorial!!
best video ive found so far on the subject, you break it down the information in easily digestible sizes. appreciate it and subbed
Preparing for my code interview tomorrow. Thank you for this video. Will most likely be brushing up more often now so I will be back :)
the most important python video I ever saw
What I recently learned in David Beazly's course, is that for 13:05 dict forming you can simply use:
d = dict(zip(names, heros))
Briliant explanation, man. Thank you very much!
Amazing tutorial! Very Helpful
very nicely explained! thanks
thanks a lot, you explained very well.
Excellent presentation ...
I am learning Python and your videos have been more than helpful. Thank you very much!
I would say your videos are so much useful. I am just getting started with python but these comprehensive videos are rapidly making to go to intermediate level of code manipulation and understanding... THANK YOU SO MUCH!!!!!!!
Wow, your video are the best out there. It's so easy to understand.
Thanks for your detailed explanation. I appreciate the efforts put by you to explain the difference between naive approach and list comprehension.
Thank yo so much Corey for all your tutorial videos! They are exactly the format I need to learn this language and coming back when I struggle doing it myself just to see that you already mentioned it. Coming from a Java backgound I really enjoy being able to do such great things so easily and having so clear instructions how to do it (right). Combining it with a Raspberry Pico and Unicorn hat makes it so much more enjoyable and motivates me to try out even more cool stuff!
Great video series. BTW: You can use a list comprehension to print the generator object as well. I'm running this in Anaconda 3.7 on Pop OS 19.04.
nums = [1,2,3,4,5]
my_nums = (n*n for n in nums)
print([n for n in my_nums])
pwd# [1, 4, 9, 16, 25]
Well done, KISS, great examples !!!
Really good tutorial, very clearly and concisely explained. Thank you!
...great way to bring into context lambda and functional - very clear/informative, excellent!!
Great job! This is way better than the for loops. Crystal clear explanation...
You are great and I am now a great fan of u