0% found this document useful (0 votes)
22 views

What Are Dict and List Comprehensions?

Dict and list comprehensions allow for the creation of new lists, dictionaries, or sets from existing iterables in a concise way. They allow performing mathematical operations, filtering, combining multiple iterables, and flattening multi-dimensional lists in a single line of Python code, saving lines of code compared to more verbose alternatives. Examples demonstrate using comprehensions to square values, only include even values, combine multiple lists, and flatten a multi-dimensional list.

Uploaded by

ameetamarwadi
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

What Are Dict and List Comprehensions?

Dict and list comprehensions allow for the creation of new lists, dictionaries, or sets from existing iterables in a concise way. They allow performing mathematical operations, filtering, combining multiple iterables, and flattening multi-dimensional lists in a single line of Python code, saving lines of code compared to more verbose alternatives. Examples demonstrate using comprehensions to square values, only include even values, combine multiple lists, and flatten a multi-dimensional list.

Uploaded by

ameetamarwadi
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

 What are Dict and List comprehensions?

Python comprehensions, like decorators, are syntactic sugar constructs that help build


altered and filtered lists, dictionaries or sets from a given list, dictionary or set. Using
comprehensions, saves a lot of time and code that might be considerably more verbose
(containing more lines of code). Let's check out some examples, where comprehensions can
be truly beneficial:
• Performing mathematical operations on the entire list
my_list = [2, 3, 5, 7, 11]

squared_list = [x**2 for x in my_list] # list comprehension


# output => [4 , 9 , 25 , 49 , 121]

squared_dict = {x:x**2 for x in my_list} # dict comprehension


# output => {11: 121, 2: 4 , 3: 9 , 5: 25 , 7: 49}
• Performing conditional filtering operations on the entire list
my_list = [2, 3, 5, 7, 11]

squared_list = [x**2 for x in my_list if x%2 != 0] # list comprehension


# output => [9 , 25 , 49 , 121]

squared_dict = {x:x**2 for x in my_list if x%2 != 0} # dict comprehension


# output => {11: 121, 3: 9 , 5: 25 , 7: 49}
• Combining multiple lists into one
Comprehensions allow for multiple iterators and hence, can be used to combine multiple lists
into one.
a = [1, 2, 3]
b = [7, 8, 9]

[(x + y) for (x,y) in zip(a,b)] # parallel iterators


# output => [8, 10, 12]

[(x,y) for x in a for y in b] # nested iterators


# output => [(1, 7), (1, 8), (1, 9), (2, 7), (2, 8), (2, 9), (3, 7), (3, 8), (3, 9)]
• Flattening a multi-dimensional list
A similar approach of nested iterators (as above) can be applied to flatten a multi-dimensional
list or work upon its inner elements.
my_list = [[10,20,30],[40,50,60],[70,80,90]]

flattened = [x for temp in my_list for x in temp]


# output => [10, 20, 30, 40, 50, 60, 70, 80, 90]
Note:  List comprehensions have the same effect as the map method in other languages. They follow
the mathematical set builder notation rather than map and filter functions in Python.

You might also like