loading...
Cover image for A guide to Python list comprehension

A guide to Python list comprehension

kalebu profile image Jordan Kalebu Updated on ・3 min read

List comprehension

Hello Pythonista, Today you’re going to learn about list comprehension, a very powerful feature of python to use when creating or filtering a list a python List based on certain constraints.

let’s get started

Imagine you want to create a list of cubic numbers from 1 to 100, Generating the cubic of numbers without using list comprehension would normally look like this.

Cubic of numbers without list comprehension

>>> cubics = []
>>> for number in range(1, 101):
...     cubic = number**3
...     cubics.append(cubic)
... 
>>> cubics
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 
 2197, 2744, 3375, 4096, 4913, 5832, 6859, 8000, 9261, 10648,
 ......
]
Enter fullscreen mode Exit fullscreen mode

Cubic of numbers using list comprehension

>>> cubics = [number**3 for number in range(1, 101)]
>>> print(cubics)
[
  1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744,
 3375, 4096, 4913, 5832, 6859, 8000, 9261, 10648, 12167, 13824, 15625,
 ....
]
Enter fullscreen mode Exit fullscreen mode

As we can see, we did the same thing with list comprehension but instead of doing it in 4 lines of code, we did it in just one. isn't cool?

*List comprehension allows you to do amazing stuff in one line (*one-liner), something that would require even more than 6 lines.

List comprehension Syntax

List = [element for element in iterable]
Enter fullscreen mode Exit fullscreen mode

Cartesian Product using List Comprehension

If A and B are two non-empty sets, then their Cartesian product A × B is the set of all ordered pairs of elements from A and B, with list comprehension you find it with one line as shown below;

>>> a = [1 , 3 , 5, 7]
>>> b = [2, 4, 6, 8]
>>> product = [(i , j) for i in a for j in b]
>>> print(product)
[(1, 2), (1, 4), (1, 6), (1, 8), (3, 2),
 (3, 4), (3, 6), (3, 8), (5, 2), (5, 4),
 (5, 6), (5, 8), (7, 2), (7, 4), (7, 6), 
 (7, 8)
]
Enter fullscreen mode Exit fullscreen mode

Conditional blocks within a list comprehension

Apart from just iterating over sequences, you can also put conditional statements such as if and else within comprehension logic to select the elements based on certain conditions.

Even number using list comprehension

Let’s say we want to make a list of even numbers from 203 to 289 using list comprehension, it can also be done in one-line as shown below;

>>> even_numbers = [number for number in range(203, 289) if number%2==0]
>>> print(even_numbers)
[
  204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230,
 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 256, 258,
 260, 262, 264, 266, 268, 270, 272, 274, 276, 278, 280, 282, 284, 286,
 288
]
Enter fullscreen mode Exit fullscreen mode

Filtering iterable using list comprehension

This an example program that uses a list comprehension to filtering candidates that are above or equal to 18.

>>> candidates = [
...     ('John', 15), 
...     ('Silyvia', 34), 
...     ('Hamis', 17), 
...     ('Alphonce', 22), 
...     ('Grace', 27)
... ]
>>> 
>>> above_18 = [candidate for candidate in candidates if candidate[1]>=18]
>>> print(above_18)
[('Silyvia', 34), ('Alphonce', 22), ('Grace', 27)]
Enter fullscreen mode Exit fullscreen mode

Loading certain files using list comprehension

Let’s use list comprehension to load all image paths in our current directory


>>> import os
>>> images = [img for img in os.listdir() if img.endswith('.jpg')]
>>> print(images)
['AI gallery.jpg']

Enter fullscreen mode Exit fullscreen mode

Flattening 2D array using list comprehension]

You can also use list comprehension to flatten the 2D array to a single 1D array by iterating on all elements on a list.


>>> data =[(1, 2), (3, 4), (5, 6)]
>>> flattened = [n for in_data in data for n in in_data]
>>> print(flattened)
[1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

Scalar Product using List Comprehension

List comprehension can also be used to find the scalar of an iterable by multiplying by a given number on each element in an iterable independently as shown below;


>>> vector = [10, 25, 32]
>>> vector3 = [n * 3 for n in vector]
>>> print(vector)
[10, 25, 32]

Enter fullscreen mode Exit fullscreen mode

Well, that’s all for today hope you find it useful, now don't be shy share now with your fellow developer.

In case of any suggestion or comment, drop it in the comment box and I will get back to you ASAP.

The Original Article can be found on kalebujordan.com

Discussion

pic
Editor guide
Collapse
michaelcurrin profile image
Mike

I've linked to your article from mine :)

dev.to/michaelcurrin/list-comprehe...

Collapse
kalebu profile image
Jordan Kalebu Author

That's okay Mike, see we both write on similar stuff

Collapse
29ff profile image
29ff

thanks for sharing!