About Lesson

List comprehension

List comprehensions are an elegant way to create new lists in Python. List comprehension is faster and more compact than normal functions and loops for creating a list in Python.

Syntax of List Comprehension

[expression for item in list]

List Comprehension vs For Loop in Python

Suppose, we want to separate the letters of the word animal and add the letters as items of a list. The first thing that comes to mind could be using for loop.

Example 1: using for loop

letters = []

for i in 'animal':


When we run the program, the output will be:

['a', 'n', 'i', 'm', 'a', 'l']

Python has an easier way to solve this issue using List Comprehension.

Example 2: using list comprehension

letters = [ letter for letter in 'animal' ]

In the above example, a new list is assigned to variable  letters, and the list contains the items of the iterable string ‘animal’. We call print() function to receive the output.

List comprehension is generally more compact and faster than normal functions and loops for creating lists.