Python Engineer

Free Python and Machine Learning Tutorials

Become A Patron and get exclusive content! Get access to ML From Scratch notebooks, join a private Slack channel, get priority response, and more! I really appreciate the support!

List Comprehension in Python

25 Jan 2020

In this Python Tutorial we will be learning about Lists Comprehension in Python. List comprehension provides a simple and concise way to create lists. It is a great feature that every Python programmer should know!

We will learn:

You can find and test the code on GitHub.

Avoid for loops!

squares = [] for i in range(5): squares.append(i * i) print(squares)

Better: Use list comprehension

# new_list = [expression for member in iterable] squares = [i * i for i in range(5)] print(squares)

An iterable can be a list, set, sequence, generator, or any other iterable.

The expression can be a function:

def cube(i): return i*i*i cubes = [cube(i) for i in range(5)] print(cubes)

Filtering

# new_list = [expression for member in iterable (if conditional)] evens = [i for i in range(20) if i%2 == 0] print(evens) def is_even(i): return i%2 == 0 evens = [i for i in range(20) if is_even(i)] print(evens)

Modifying

# new_list = [expression (if else conditional) for member in iterable] a = [1, 2, 3, 4, 5, 6, 7, 8, 9] b = [10 if i > 5 else 0 for i in a] print(b)

Set comprehension

quote = "hello everybody" unique_vowels = {i for i in quote if i in 'aeiou'} print(unique_vowels) squares = {i: i * i for i in range(5)} print(squares)

Nested list comprehension

Use sparely! This can be more confusing most if the times.

matrix2d = [[i*j for i in range(5)] for j in range(1,3)] print(matrix2d)

List comprehension vs generator

A list comprehension in Python works by loading the entire output list into memory -> use genereator for large data!
Generators can also be created with generator expressions:

# new_generator = (expression for i in iterable) s = sum([i * i for i in range(1000)]) print(s) s = sum((i * i for i in range(1000))) print(s) import sys l = [i * i for i in range(1000)] print(sys.getsizeof(l), "bytes") g = (i * i for i in range(1000)) print(sys.getsizeof(g), "bytes")

A word about speed:

Somtetimes it can be faster, but this may not always be the case!

from timeit import default_timer as timer start = timer() a = [i*i for i in range(1_000_000)] stop = timer() print(f'{stop-start:.4f} seconds') start = timer() a = [] for i in range(1_000_000): a.append(i*i) stop = timer() print(f'{stop-start:.4f} seconds')