Discover the power and efficiency of Python list comprehensions in this detailed guide. Learn how this feature can be twice as fast as traditional Python modules and methods, while effectively iterating through lists and managing data.
Key Insights
- The video explains how to use Python list comprehensions, which are often avoided due to confusion but are actually very powerful and easy to use.
- By using list comprehensions instead of a common method like 'append,' one can achieve results twice as fast thanks to the way append is implemented as a module.
- List comprehensions are always written within square brackets, indicating the return of a list.
- These comprehensions can be used to filter through a list, identify specific elements, and append them to a new list.
- List comprehensions can also be used to perform operations on list items, such as multiplying the items by a certain number.
- Understanding and implementing list comprehensions in Python can lead to more efficient coding practices and faster data processing.
In this video, we're going to look at how to use List Comprehension in Python
Video Transcription
Hi, my name is Art and today we're going to be talking about Python list comprehensions. Most of the time people don't use list comprehensions because it can be confusing. In this short video, let me explain how to use it. It's very powerful and easy to use.
First, let's use a simple example. I'm going to create a Python list and suppose here I have a couple of numbers and many fives. And suppose my goal is to create a new list and go through first list, and if I find a 5, I want to append it to the new list.
So, pretty simple task. We need to use a for loop, so "for n in a_list". Then, I'm gonna create a filter "if n == five". Then, what I want to do next is add something to a list. So, here is going to be append, and I'm going to append n if that's the case.
I'm going to run this and let's check our new list. And our new list contains only fives, right? So, that's pretty much how most people would do this task. But we could go much faster. Why? And how? Because append is implemented as a module. So, every time this filter returns true, append should be downloaded from Python.
Instead of using append and going twice as fast, we could rewrite this as a list comprehension. List comprehension is always written within square brackets. Why? Because we want to return a list. I'm going to copy it from here so you could see the logic and how simple it is.
In square brackets, I'm going to copy "for n in a_list". We don't need a colon. Then, we need to use a filter "if n == five". So, filter goes here. Then, an expression—what will be an expression? An expression will be "n".
So, in this case, I'm getting exactly the same result. You might ask me what's the difference? The difference is that list comprehension is twice as fast. The only reason for that is because we're not using the module append and we're not using the modular pen. Because list comprehension is wrapped in these square brackets, by default it will be returned as a list.
Let me show you the same task again. But this time, let's add a couple of eights. So, 888. Now, I'm going to do exactly the same from scratch and I'm going to assign it to a variable name—let's say "e" for eight.
So, then I use square brackets because it's a list comprehension. Then, I need to use a for loop, because I need to iterate through the original list. So, "for number in a_list". Then, we can use a filter if we need one. So, "if number == eight".
Then, if that's the case, what do we want to do? We want to have that number. Let me stop right here. Let's see what our "e" is. You see, this comprehension always returns a list. You see in square brackets means a list full of eights. Why? Because here, we have this filter if the number equals eight.
Sometimes, you want to do something—maybe if there are eight, maybe you want to multiply it by a hundred. So, in this expression, you could multiply by 100. Now, we're getting this 800,800,800.
Again, list comprehension is another way to iterate through a list and do something with items from the list. Again, list comprehension is much faster—as a matter of fact, twice as fast as using Python module and the method append. Because append is a method of that object list.
I hope from now on you'll be using list comprehensions. If you want to learn more, watch my other videos. I'll see you in the next video.