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

КОМЕНТАРІ • 498

  • @just_living_
    @just_living_ 8 місяців тому +33

    8 years after and my man is still better than 90% of other tutorial, cheers man

  • @zackm7541
    @zackm7541 5 років тому +233

    Corey I hope you always go to bed feeling good about your work and helping out the community. Thanks for your very clear tutorials.

    • @aayush_7037
      @aayush_7037 3 роки тому +4

      Positivity ❤️❤️

    • @dietrevich
      @dietrevich 3 роки тому

      I bet he would feel and sleep even better if you supported him.

  • @SSCrow
    @SSCrow 9 років тому +349

    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!

    • @coreyms
      @coreyms  9 років тому +17

      +Crow Thanks for the kind words. I appreciate it!

    • @UrsuIrinel
      @UrsuIrinel 8 років тому +6

      +Crow I agree with this. He really knows how to explain things.

    • @copyrightedchannel4939
      @copyrightedchannel4939 3 роки тому

      5 years

    • @lochlanvihaan6892
      @lochlanvihaan6892 3 роки тому

      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

  • @jeffreystockdale8292
    @jeffreystockdale8292 7 років тому +16

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

  • @kerwinso
    @kerwinso 4 роки тому +57

    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!

    • @sandeepanbhattacharyya828
      @sandeepanbhattacharyya828 3 роки тому +1

      yes so true. I came to learn list comprehensions now going back with even more comprehensions :)))

  • @ajith1804
    @ajith1804 5 років тому +6

    To be frank,this is the best video for learning about list comprehension on UA-cam!

  • @buzz.b
    @buzz.b Рік тому +4

    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.

  • @RandomShowerThoughts
    @RandomShowerThoughts 6 років тому +8

    the amount of material I have learned from this channel is literally amazing!

  • @MegaJohn144
    @MegaJohn144 6 років тому +11

    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.

  • @kedwards127
    @kedwards127 5 років тому +2

    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.

  • @uiato
    @uiato 4 роки тому +2

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

  • @mateuszpolanski3162
    @mateuszpolanski3162 3 роки тому +1

    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.

  • @faturotivincenttemitope4709
    @faturotivincenttemitope4709 7 місяців тому +2

    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.

    • @tommyakasha9156
      @tommyakasha9156 3 місяці тому

      hey man, same here, why? already solve?

  • @jehushaphat
    @jehushaphat 3 роки тому +1

    This was the most comprehensive video I've seen on comprehension.

  • @VMSaloudi
    @VMSaloudi 8 років тому +43

    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.

    • @mariaaraujo3042
      @mariaaraujo3042 6 років тому

      well, he still is a good teacher!

    • @nemplayer1776
      @nemplayer1776 5 років тому

      @@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.

  • @vinc9846
    @vinc9846 9 років тому +32

    at 12:00 you are able to create dict from zip:
    dict(zip(names, heros))

    • @SavagePotatoo
      @SavagePotatoo 6 років тому +3

      Yes, that is a way of doing it, but his method is more pythonic.

    • @liangjiang3122
      @liangjiang3122 5 років тому

      is that python2 only or for both python2 and python3?

    • @rebelScience
      @rebelScience 5 років тому

      @@liangjiang3122 Both, but you might get unexpected result, like the one in a commend above.

  • @riyan5259
    @riyan5259 4 роки тому +3

    I'm so mad at the viewers for not subscribing.. Mate you DESERVE a million subscribers.

  • @amandaahringer7466
    @amandaahringer7466 2 роки тому +2

    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 :)

  • @shazkingdom1702
    @shazkingdom1702 5 років тому +4

    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. 👨‍🏫

  • @sahilsharma-hj4gq
    @sahilsharma-hj4gq 5 років тому +2

    oh mannnnnn....... simply AWESOME.. You teach way better than most of the python tutors. Your detailed explainations are dope.

  • @olee_7277
    @olee_7277 5 років тому +118

    I know Im immature but every time someone calls a dictionnary variable : my_dict I burst out laughing

    • @gleidonn3735
      @gleidonn3735 3 роки тому +1

      Ahahahahahahahahahahahahahahahahha so funny.

    • @nitinsinghbrijwal3183
      @nitinsinghbrijwal3183 3 роки тому +1

      12:28

    • @seanli75
      @seanli75 3 роки тому

      LOLZ 😂😂😂😂😂😂🤣🤣🤣🤣🤣🤣🤣🤣🤣😆😆

    • @dong8912
      @dong8912 3 роки тому

      Help I cant find the unsee button!

    • @samhouston2000
      @samhouston2000 3 роки тому +1

      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.

  • @tongliu1076
    @tongliu1076 5 років тому +2

    I watched every topic video before the topic my class is going to reach. It makes my class so easy.

  • @oleksiy.tryfonov8
    @oleksiy.tryfonov8 Рік тому +1

    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.

  • @Ryebanana
    @Ryebanana 5 років тому +1

    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.

  • @rotrose7531
    @rotrose7531 5 років тому +5

    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.

  • @AlexKashie
    @AlexKashie Рік тому

    Thank you Corey, content made 7 years ago and still fresh.

  • @nasalee3729
    @nasalee3729 6 років тому +101

    04:15
    python 3.6 should this:
    my_list = list(map(lambda n: n * n, nums))

  • @RC-bl2pm
    @RC-bl2pm 6 років тому +1

    Boy you're a good teacher! Great explanation followed by meaningful demonstration. Clear.Punchy. Keep going I'll keep watching.

  • @aisolutions834
    @aisolutions834 2 роки тому

    Wow, a concise video teaching elegantly creating lists, dictionaries, sets, and generators!

  • @simondolapo1567
    @simondolapo1567 2 роки тому

    All I can say is amazing contents never die...They still live for decades to come

  • @mandeepubhi4744
    @mandeepubhi4744 5 років тому +2

    An elegant approach of illustration, Beautiful!!!

  • @chigglywiggly
    @chigglywiggly 5 років тому +1

    Best series of videos on Python on the internet.

  • @Gameplays4u2
    @Gameplays4u2 5 років тому +2

    OMG you made these concepts to simple it took me forever to understand generators till I found your video.

  • @Abcsciencemusic
    @Abcsciencemusic 3 роки тому

    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

  • @database_tips_tricks
    @database_tips_tricks 2 роки тому

    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

  • @novicetech1
    @novicetech1 6 років тому +2

    Wow! Now I finally understand list comprehensions. Thanks Corey. Another great tutorial.

  • @davidtodd9918
    @davidtodd9918 4 роки тому +1

    Thanks. I had not heard of set comprehension or generator creation using parens, and I appreciated the excellent refresher on other topics!

  • @aposaheed4543
    @aposaheed4543 2 роки тому

    You are the best tutor for me. I understand ur style than anyone. Thanks

  • @emperornugz89
    @emperornugz89 Рік тому

    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!

  • @AundreL
    @AundreL 4 роки тому

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

  • @expat2010
    @expat2010 3 роки тому

    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.

  • @jialiangpan7258
    @jialiangpan7258 2 роки тому

    This is by far the best tutorials on list comprehension I’ve ever got online. Thank you so much!

  • @JAREMAIALOOK
    @JAREMAIALOOK 5 років тому +2

    I am very grateful that you make these videos, keep up the great work!

  • @iiit2tech
    @iiit2tech 4 роки тому +1

    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.

  • @difegam3
    @difegam3 4 роки тому +1

    Unfortunately it is not possible give you more than 1 like. You are the best man! Thanks Corey.!

  • @spizeck
    @spizeck 8 років тому +4

    Brilliantly done. You were very well prepared and it showed. Bravo sir.

  • @peterchen5710
    @peterchen5710 5 років тому +1

    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!

  • @ezraanna1
    @ezraanna1 5 років тому +1

    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!

  • @24junar
    @24junar 7 років тому +2

    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

  • @saradeeee
    @saradeeee 7 років тому

    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

  • @jpm4689
    @jpm4689 3 роки тому +1

    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.

  • @djcoolio8096
    @djcoolio8096 5 років тому +2

    The best explanation ever! You're making our lives so much easier! Thanks!

  • @ROC4Life96
    @ROC4Life96 5 років тому +1

    This wasn't what i was looking for, BUT i am very glad i stumbled upon this video! Thank you!

  • @tunkyi7162
    @tunkyi7162 5 років тому +2

    Thank you bro. Your lectures are GOLD!

  • @arthuralexandersson
    @arthuralexandersson 2 роки тому

    Very useful even after 6 years, thank you!

  • @ArunKumar-qy6fm
    @ArunKumar-qy6fm 6 років тому +2

    Explained wonderfully. Thank you.

  • @prasadp8265
    @prasadp8265 4 роки тому

    Best Python tutorials in youtube!

  • @deepgsingh
    @deepgsingh 5 років тому +2

    Awesome, I have watched all of your python videos and they are great stuff , Thank you

  • @DeanG621
    @DeanG621 4 роки тому

    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.

  • @TheKaushal8686
    @TheKaushal8686 6 років тому +2

    best python videos on youtube..

  • @sudhakannan3080
    @sudhakannan3080 5 років тому +1

    Very good video to understand list comprehensions

  • @SeekHeart
    @SeekHeart 8 років тому +40

    Hey Corey, could you do a video on lambda, map, reduce and those family of lovelies?

    • @coreyms
      @coreyms  8 років тому +38

      +SeekHeart Sure thing. Just wrote that down. I'll try to make a video on those in the near future.

    • @SeekHeart
      @SeekHeart 8 років тому +4

      Thanks man!

    • @samsammurphy
      @samsammurphy 7 років тому +2

      +1

    • @jagannathgaidhani5333
      @jagannathgaidhani5333 6 років тому

      yes please make a video over that

    • @sushichanel7299
      @sushichanel7299 6 років тому +1

      we love to get immediately level when we finish this course. Thanks so much for your courses.

  • @ppant
    @ppant 6 років тому +2

    You are a great teacher. Keep it up! Thanks

  • @anotter_one
    @anotter_one 4 роки тому +1

    Man, you're the real MVP. Thanks a bunch.

  • @darrenandveronica
    @darrenandveronica 8 років тому +1

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

  • @soumyazyx
    @soumyazyx 4 роки тому +1

    Thank you Corey for your time and effort. Keep spreading the knowledge

  • @theegreatestever2420
    @theegreatestever2420 2 роки тому

    ABSOLUTELY AMAZING EXPLANATIONS!

  • @user-yk9rr5ir7v
    @user-yk9rr5ir7v Рік тому

    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.

  • @eldarmammadov7872
    @eldarmammadov7872 Рік тому

    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

  • @HappyAnimals3D
    @HappyAnimals3D 5 років тому +2

    This was very informative. Your examples helped a great deal.

  • @amaarquadri
    @amaarquadri 3 роки тому

    I saw a dictionary compression for the first time today and was super amazed I had never encountered it. Eventually led to this video!

  • @akira_asahi
    @akira_asahi 2 роки тому

    Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

  • @raat1
    @raat1 5 років тому +2

    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

  • @ashj1165
    @ashj1165 6 років тому +2

    Best video on lists.

  • @boxcubestudios
    @boxcubestudios 5 років тому +1

    Fantastic teaching!

  • @hacninetushar
    @hacninetushar 3 роки тому

    Whenever fall problems in programming I searched that problem in your channel.

  • @antonioburke7716
    @antonioburke7716 6 років тому +2

    You are the best on youtube, God thank you.

  • @harnathatmakuri2394
    @harnathatmakuri2394 5 років тому +2

    learned a lot from this best video ever seen thank u

  • @trevordontstalkme1303
    @trevordontstalkme1303 3 роки тому

    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.

  • @TheJAM_Sr
    @TheJAM_Sr 2 роки тому

    Dude you are a rockstar! Thank you for making this easy to follow tutorial!!

  • @kwrifles
    @kwrifles 2 роки тому

    best video ive found so far on the subject, you break it down the information in easily digestible sizes. appreciate it and subbed

  • @firstfiverugby
    @firstfiverugby 3 роки тому

    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 :)

  • @jonathandaniel7321
    @jonathandaniel7321 4 роки тому +1

    the most important python video I ever saw

  • @yomajo
    @yomajo 2 роки тому

    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))

  • @GuilhermeMendesG
    @GuilhermeMendesG 3 роки тому

    Briliant explanation, man. Thank you very much!

  • @priyankkharat7407
    @priyankkharat7407 6 років тому +2

    Amazing tutorial! Very Helpful

  • @anuradhapani1918
    @anuradhapani1918 8 років тому +5

    very nicely explained! thanks

  • @aminzaiwardak6750
    @aminzaiwardak6750 4 роки тому +1

    thanks a lot, you explained very well.

  • @bulldawg4498
    @bulldawg4498 5 років тому +2

    Excellent presentation ...

  • @ProbuddhaNag
    @ProbuddhaNag 8 років тому

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

  • @jaroffdaniel5242
    @jaroffdaniel5242 2 роки тому

    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!!!!!!!

  • @peternguyen6266
    @peternguyen6266 3 роки тому

    Wow, your video are the best out there. It's so easy to understand.

  • @whimsicalkins5585
    @whimsicalkins5585 2 роки тому

    Thanks for your detailed explanation. I appreciate the efforts put by you to explain the difference between naive approach and list comprehension.

  • @bartuslongus
    @bartuslongus 2 роки тому

    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!

  • @TheDouglasTrevino
    @TheDouglasTrevino 4 роки тому

    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]

  • @bgable7707
    @bgable7707 3 роки тому

    Well done, KISS, great examples !!!

  • @battlemode
    @battlemode 7 років тому +1

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

  • @zzspenz7492
    @zzspenz7492 8 років тому

    ...great way to bring into context lambda and functional - very clear/informative, excellent!!

  • @lamarts100
    @lamarts100 8 років тому

    Great job! This is way better than the for loops. Crystal clear explanation...

  • @immanuel4747
    @immanuel4747 4 роки тому +1

    You are great and I am now a great fan of u