logo


comprehensions


<p>Python has a system that might simplify your code called comprehensions. They allow you to turn your nested for loop into an amazing single line of code. In this series of videos we'll highlight some useful ways to use them.</p>


1 - Introduction
2 - If
3 - Enumerate
4 - Example
5 - Nested
6 - Dict
7 - Unique
8 - Unpack
9 - Zip

Let's say we want to create a new list from an old list.

old_list = [1, 2, 3, 4, 5]
new_list = []
for i in old_list:
    new_list.append(i * 2)
new_list

In this case we're doubling numbers. This simple operation can also be done on a single line instead.

[i * 2 for i in old_list]

In this series of video's we'll explore this coding pattern.