List Comprehension in Python

Free Video Tutorial and Guide

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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.

photo of Noble Desktop

Noble Desktop

At Noble Desktop you can learn how to code websites, build iOS apps, make graphics, and more. From our front-end coding bootcamp to Photoshop classes, we offer a variety of comprehensive day, evening, and weekend training classes as well as certificate programs in web design, web development, HTML email, and more. Courses are available in-person (at our training facility in Soho), live online, and customized corporate or private training. Our course materials and workbooks are used by colleges and schools worldwide. Learn a skill today and start using it tomorrow.

More articles by Noble Desktop

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram