Calmcode - comprehensions: introduction

Introduction

1 2 3 4 5 6 7 8 9

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.

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.