List Comprehension - BEST Python feature !!! Fast and Efficient

188,212
0
Published 2022-07-01
In this tutorial, we will learn all about list comprehensions and how they can make our Python journey much easier and much more efficient!! 🤓🤓🤓
List comprehensions provide a shorter and faster alternative to "for" loops (especially when combined with conditional statements as well as append commands). And best of all - it takes only 1 LINE OF CODE! 😱
This video offers 3 separate coding exercises, various examples and colorful illustrations of the concepts involved. Please feel free to use my code and artwork, you can find it in the link below:

⭐⭐ clone my code: ⭐⭐
* Sorry code is unavailable, RIP Wayscript 😭😭😭*

And on a festive note...

🍁HAPPY CANADA DAY!!!🍁
to all my fellow Canadians! (and other Earth dwellers who celebrate 😉)
Sending you lots of love from beautiful British Columbia! 💗💗💗

⏰ time stamps ⏰
____________________
00:00 - intro
00:17 - what is a list comprehension?
01:22 - for loop vs list comprehension
02:40 - coding example
06:07 - conditional list comprehension
06:27 - coding example 2
09:02 - list comprehension on strings
09:29 - coding example 3
12:09 - else if list comprehension
13:50 - coming soon: dictionary comprehension
14:12 - thanks for watching!

🐍🐍 related tutorials of mine 🐍🐍
_____________________________________
- For Loops:
   • Python For Loops - Programming for Be...  

➡️➡️ starter code ⬅️⬅️
__________________________
fruits = ["apples", "bananas", "strawberries"]

for fruit in fruits:
print(fruit)

💳💳 credits 💳💳
_______________________
- Beautiful Icons are by:
www.flaticon.com/

- Beautiful Like/Share/Subscribe video graphics by:
mixkit.co/

🤝 connect with me 🤝
_______________________
- Github: github.com/MariyaSha
- Discord: discord.gg/wgTTmsWmXA
- LinkedIn: www.linkedin.com/in/mariyasha888/
- Twitter: twitter.com/MariyaSha888
- Blog: pythonsimplified.ca/

All Comments (21)
  • Using list comprehension for print is a very bad example. Compared to the the ordinary for-loop, an implicit list of return values from print is generated. Also the statement, that append(...) isn't used in list comprehension isn't true. It's not explicitly written as code, but somehow, elements need to be added to the list. The presentation states that some code is faster than other code, but no evidence is shown e.g. by using timeit. boolean values shouldn't be compared (==) with True. At first use "is True". At second, a call to the "is" operator can be skipped, because a boolean can be used directly in an if statement. Moreover there is no if-statement needed, as booleans can be directly converted to integers by calling int(...). list comprehension for strings ... I have no words how silly this is. --------------- Sorry, but such tutorials are the root cause for so many bad Python code in the world. Just because you can do it, doesn't mean you should do it; neither you should teach others ! There are many good use cases for list comprehensions. There are also lots of use cases when list comprehensions are more compact, more readable and way faster than ordinary loops. But the given examples in this tutorial are neither.
  • Thank you so much for all the lovely comments folks! I'm off to a Canada day camping trip deep in the wilderness, will catch up with all your messages once I'm back! 😃 (including on Twitter, LinkedIn and Discord 😉)
  • @guycostco7516
    You are a life saver. It is so much easier learning Python with your fantastic tutorials. Keep em coming! Cheers from Montreal.
  • @jlindsley9288
    Excellent tutorial. List comprehension is one of those things you think you understand until you try to explain it to someone. Always good to refresh your knowledge.
  • @Tooxcade
    Absolutely a fantastic teacher. I like the way you explained it. Thank you!
  • @katbryce
    Some additional things I find useful: For more complicated list comprehensions, create a function that takes the list item as a single argument, and returns the result, then use that in the list comprehension in much the same way as you used print(). If you have a really huge list, I sometime deal with lists that have 10m+ items in them, then instead of using list comprehension, use multiprocessing.Pool().map(function,list) to use all of your available CPU cores. This doesn't seem to work so well in Windows, but works very well in Linux and FreeBSD.
  • This was AWESOME!! I really like the way you simplified things. Great examples. Will love to see the same using dictionaries. Thanks so much!!
  • Always thought you had excellent teaching skills. The new slick look to these tutorials makes this the best place on the internet for (true) beginner Python instruction. Really looking forward to this channel going insanely viral in the future.
  • @rahulkmail
    Excellent. The way you covered the example's, just mind blowing.
  • @MegaJohn144
    I have read and watched a lot of tutorials about Python list comprehension. This was the best. I think now I can write a list comprehension without copying somebody else's code.
  • Great tutorial, I'd heard of list comprehension, but hadn't really understood it until you explained it. I can't wait to start using this now!
  • @sasuke21153
    I stopped using python for almost 2 months(not really long) but I was looking for some quick refresher with the language, and your videos really helped me a lot. I just want to say thank you so much!!!!.
  • I love how you edit your videos. Must be a lot of work but it pays off. Great to watch and learn!
  • @jamvin5647
    I like this tutorial because it was easy to follow along and understand the basic ideas behind list comprehensions. This is the video I was needing, thank you!
  • You've done the entire course with much practical and super exiting way. so keep doing more videos and you are absolutely gorgeous!
  • @rexsu9202
    Love the way you teach ! Thanks !!!!
  • @blevenzon
    So much to learn so little time and brain. Thank you 🙏