Comprehensions

Comprehensions

We can define comprehensions as constructs that allow sequences to be built from other sequences. In Python, comprehensions offer a concise and short way of constructing new sequences like set, dictionary, lists, etc. The following are the common types of comprehensions supported by Python:

  1. Dictionary Comprehensions – came with Python 3.0
  2. Set Comprehensions – came with Python 3.0
  3. List comprehensions – introduced in Python 2.0

We have briefly discussed them below:

List Comprehensions

They provide an excellent way of creating new lists. The structure of a list comprehension is:

Output_list = [output_exp for var in Input_list if (var satisfies this condition)]

A list comprehension may or may not contain an if condition. Also, they can contain multiple for or nested list comprehensions. Consider the examples below:

Example

If we want to create an output list that contains even numbers present in the input list. We can do this using either list comprehension or for loops as shown below:

  • The first method is using for loops

# Using for loops to construct output list

Input_list = [4, 4, 5, 6, 7, 8, 9, 9,]

Output_list = [ ]

#Using the for loop to construct output list

for var in input_list:

If var %2 == 0:

output _list.append (var)

print (“For loop output list :”, output_list)

The output produced when we run this program is:

For loop output list: [4, 4, 6, 8]

  • The second method is using list comprehensions:

# Using list comprehensions to construct output list

Input_list = [4, 4, 5, 6, 7, 8, 9, 9,]

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

Print (“List comprehension output list :” list_using_comp)

The output produced when we run this program is:

List comprehension output list: [4, 4, 6, 8]

From the two examples, you can decide which method suits better

Dictionary Comprehensions

We can extend the idea of list comprehensions by creating a dictionary using dictionary comprehensions. Here is how the basic structure of a dictionary comprehension looks like:

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

Here are sample programs illustrating how to use for loop and dictionary comprehension to create output dictionary:

Example

Create an output dictionary that contains odd numbers that are present in the input list as keys and their cubes as values:

For this kind of task, we can use:

  • The for loops

#using for loop to construct output dictionary

Input_list = [3, 4, 5, 6, 8, 9,]

Output_dict = { }

For var in input_list:

If var %2! = 0:

Output_dict [var] = var**3

Print (“For loop output dictionary:”, output_dict)

The output generated is:

For loop output dictionary: {3 : 27, 5 : 125, 9 : 729}

  • Using dictionary comprehensions

# Constructing output dictionary using dictionary comprehensions

Input_list = [3, 4, 5, 6, 8, 9,]

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

Print (“Dictionary comprehension output list:”, dict_using_comp)

The output generated is:

Dictionary comprehension output list: {3 : 27, 5 : 125, 9 : 729}

Set comprehensions

Set comprehensions are almost similar to list comprehensions. The only difference is that set comprehensions use curly brackets while list comprehensions do not. The example below will help you understand set comprehension better:

Example

Create an output set which contains only the even numbers in the input list:

You should note that set discards all duplicate values. Here is how we can complete this task using for loops and set comprehension

  • Using for loop

# Using for loop to construct output set

Input_list = [ 2, 2, 4, 4, 5, 6, 6, 7, 7]

Output_set = set ( )

For var in input_list:

If var % 2 == 0:

Output_set.add (var)

Print (“For loop output : “, output_set)

Output generated is:

For loop output: {2, 4, 6}

  • Using set comprehensions

# Constructing output set using set comprehensions

Input_list = [ 2, 2, 4, 4, 5, 6, 6, 7, 7]

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

Print (“Set comprehensions output list:”, set_using_comp)

Output generated is:

Set comprehensions output list: {2, 4, 6}

Are you struggling with assignments related to comprehensions? Avail of Python assignment help from Programmingassignmenthelper.com today. Our experts are known to curate precise and error-free codes that can earn you decent grades.