Understandings in Python

Understandings in Python provide us with a short and concise way to build new sequences (such as lists, sets, dictionary, etc.) using sequences that have already been defined. Python supports the following 4 types of comprehension:

  • List of Understandings
  • Dictionary of Concepts
  • Set Understanding
  • Understanding Generator

List of Comprehensions:

List Comprehensions provide an elegant way to create new lists. Below is the basic structure of a list comprehension:

output_list = [ output_exp for var in input_list if ( var satisfies this condition)]

Note that a list comprehension may or may not contain an if condition. Lists can contain multiple for (nested list).

Example # 1: Suppose we want to create an output list that contains only the even numbers present in the input list. Let`s see how to do this using for loops and list comprehension and decide which method works best.

# Building the output list WITHOUT
# Using list views

input_list = [ 1 , 2 , 3 , 4 , 4 , 5 , 6 , 7 , 7 ]

 

output_list = []

  
# Using a loop to build an output list

for var in input_list:

if var % 2 = = 0 :

output_list.append (var)

  

print ( "Output List using for loop:" , output_list)

Output:

 Output List using for loop: [2, 4, 4, 6] 

# Using list views
# to build the output list

 

input_list = [ 1 , 2 , 3 , 4 , 4 , 5 , 6 , 7 , 7 ]

 

 

list_using_comp = [var for var in input_list if var % 2 = = 0 ]

 

print ( " Output List using list comprehensions: " ,

list_using_comp)

Exit :

 Output List using list comprehensions: [2, 4, 4, 6] 

Example # 2: Suppose , we want to create an output list that contains the squares of all numbers from 1 to 9. Let`s see how to do this using loops and a list comprehension.

# Building output list using a for loop

output_list = []

for var in range ( 1 , 10 ):

output_list.append (var * * 2 )

 

print ( "Output List using for loop:" , output_list)

Output :

 Output List using for loop: [1, 4, 9, 16, 25, 36, 49, 64, 81] 

# Building the output list
# using the comprehension list

list_using_comp = [var * * 2 for var in range ( 1 , 10 )]

 

print ( "Output List using list comprehension:"

list_using_comp)

Output :

 Output List using list comprehension: [1, 4, 9, 16, 25 , 36, 49, 64, 81] 

Dictionary of concepts:

Extending the idea of ​​list representations, we can also create a dictionary using dictionary representations. The basic structure of vocabulary comprehension is as follows.

output_dict = {key: value for (key, value) in iterable if (key, value satisfy this condition)}

Example # 1: Suppose we want to create an output dictionary that contains only odd numbers that appear in the input list as keys , and their cubes — as values. Let`s see how to do this using loops and a dictionary.

input_list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]

 

output_dict = {}

 
# Using a loop to build the output dictionary

for var in input_list:

if var % 2 ! = 0 :

output_dict [var] = var * * 3

 

print ( "Output Dictionary using for loop:" ,

output_dict)

Exit:

Output Dictionary using for loop: {1: 1, 3: 27, 5: 125, 7: 343}

# Using vocabulary understanding
# to build the output dictionary

 

input_list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]

  

dict_using_comp = {var: var * * 3 for var in input_list if var % 2 ! = 0 }

 

print ( "Output Dictionary using dictionary comprehensions:" ,

dict_using_comp)

Output :

 Output Dictionary using dictionary comprehensions: {1: 1, 3: 27, 5: 125, 7: 343}  

Example # 2: Given two lists containing the names of states and their corresponding capital letters, create a dictionary that maps states to their corresponding capital letters. Let`s see how to do this using loops and a dictionary.

state = [ `Gujarat` , ` Maharashtra` , `Rajasthan` ]

capital = [ ` Gandhinagar` , `Mumbai` , `Jaipur` ]

  

output_dict = {}

 
# Using a loop to build an output dictionary

for (key, value) in zip (state, capital):

output_dict [key] = value

 

print ( "Output Dictionary using for loop:" ,

output_dict)

Output:

 Output Dictionary using for loop: {`Gujarat`:` Gandhinagar`, `Maharashtra`:` Mumbai`, `Rajasthan`: `Jaipur`} 

# Using dictionary comprehension
# to build an output dictionary

 

state = [ `Gujarat` , ` Maharashtra` , `Rajasthan` ]

capital = [ ` Gandhinagar` , `Mumbai` , ` Jaipur` ]

 

dict_using_comp = {key: value for (key, value) in zip (state, capital )}

 

print ( "Output Dictionary using dictionary comprehensions:"

dict_using_comp)

Output :

 Output Dictionary using dictionary comprehensions: {`Rajasthan`:` Jaipur`, `Maharashtra`:` Mumbai`, ` Gujarat`: `Gandhinagar`} 

Set understandings:

Set understandings are very similar to list ones. The only difference between the two is that the set of understandings use curly braces {} . Let`s take a look at the following example to understand the set of understandings.

Example # 1: Suppose we want to create an output set that contains only the even numbers present in the input list. Note that set discards all duplicate values. Let`s see how we can do this using for loops and establish comprehension.

input_list = [ 1 , 2 , 3 , 4 , 4 , 5 , 6 , 6 , 6 , 7 , 7 ]

 

outpu t_set = set ()

 
# Using a loop to build an output set

for var in input_list:

if var % 2 = = 0 :

output_set.add (var)

 

print ( "Output Set using for loop:" , output_set)

Output :

 Output Set using for loop: {2, 4, 6}  

# Using Set understanding
# to build an output set

 

input_list = [ 1 , 2 , 3 , 4 , 4 , 5 , 6 , 6 , 6 , 7 , 7 ]

 

set_using_comp = {var for var in input_list if var % 2 = = 0 }

 

print ( " Output Set using set comprehensions: " ,

  set_using_comp)

Exit:

 Output Set using set comprehensions: {2, 4, 6} 

Comprehension Generator:

Generator Comprehensions are very similar to list comprehensions. One of the differences between the two is that the generators use parentheses, while the list — square. The main difference between the two is that generators do not allocate memory for the entire list. Instead, they generate each value one by one, so they use memory efficiently. Let`s look at the following example to understand the generator`s understanding:

input_list = [ 1 , 2 , 3 , 4 , 4 , 5 , 6 , 7 , 7 ]

 

output_gen = (var for va r in input_list if var % 2 = = 0 )

  

print ( "Output values ​​using generator comprehensions: " , end = ` ` )

 

for var in output_gen:

print (var, end = `` )

Output:

 Output values ​​using generator comprehensions: 2 4 4 6