Python 3x Pandas Django

List\Set\Dict Comprehension


Python comprehension offers an alternative syntax to creating lists and other sequential data types. While other methods of iteration, such as for loops, can also be used to create lists, list comprehension may prefer because it offers a shorter syntax when you want to create a new list based on the values of an existing list also they can limit the number of lines used in your program.

Usual For Loop:

new_list = []
for item in 'Hellloooo':
    new_list.append(item)
print(new_list)   # ['H', 'e', 'l', 'l', 'l', 'o', 'o', 'o', 'o']

List Comprehension

List comprehensions consist of an iterable containing an expression followed by a for clause. This can be followed by additional for or if clauses, so familiarity with for loops and conditional statements will help you understand list comprehensions better.

Syntax

list_variable = [item for item in iterable]

Example 1:

new_list = [item for item in 'Hellloooo']
print(new_list) # ['H', 'e', 'l', 'l', 'l', 'o', 'o', 'o', 'o']

Example 2:

Create a list with a range(1,50)

new_list = [item for item in range(1,50)]
print(new_list)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49]

Example 3: With Condition

Create a list divisible by 2 in a range(1,50)

new_list = [item for item in range(1,50) if item % 2 == 0]
print(new_list)

Output:

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32,
 34, 36, 38, 40, 42, 44, 46, 48]

Example 4: With Expresssion

Create a list square a number in a range(1,50)

new_list = [item * item for item in range(1,50)]
print(new_list)

Output:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196,
 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676,
 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369,
 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209,
 2304, 2401]

Example 4: With Expresssion & Condition

Create a list square a even number in a range(1,50)

new_list = [item * item for item in range(1,50) if item % 2 == 0]
print(new_list)

Output:

[4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676,
 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936, 2116, 2304]

Set Comprehension

Set comprehension is similar to list comprehension but it uses {} curly braces and it follows set properties like no duplicate, unordered...

Example 1:

new_list = {item for item in 'Hellloooo'}
print(new_list) # {'H', 'e', 'o', 'l'}

Example 2:

Create a list with a range(1,50)

new_list = {item for item in range(1,50)}
print(new_list)

Output:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49}

Example 3: With Condition

Create a list divisible by 2 in a range(1,50)

new_list = {item for item in range(1,50) if item % 2 == 0}
print(new_list)

Output:

{2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32,
 34, 36, 38, 40, 42, 44, 46, 48}

Example 4: With Expresssion

Create a list square a number in a range(1,50)

new_list = {item * item for item in range(1,50)}
print(new_list)

Output:

{256, 1, 1024, 2304, 4, 900, 1156, 9, 16, 144, 400, 529, 784, 1296, 1681,
 1936, 25, 289, 2209, 36, 676, 1444, 169, 49, 441, 1849, 64, 576, 961,
 1089, 196, 324, 1600, 2116, 841, 1225, 81, 729, 1369, 225, 2401, 100, 484,
 1764, 361, 2025, 625, 1521, 121}

Example 4: With Expresssion & Condition

Create a list square a even number in a range(1,50)

new_list = {item * item for item in range(1,50) if item % 2 == 0}
print(new_list)

Output:

{256, 1024, 2304, 4, 900, 1156, 16, 144, 400, 784, 1296, 1936, 36, 676,
 1444, 64, 576, 1600, 196, 324, 2116, 100, 484, 1764}

Dict Comprehension

Dictionary comprehension is an elegant and concise way to create dictionaries.

Syntax

dictionary = {key: value for key, value in iterable (refer example 4)}

Example 1:

my_dict = {'a':1,
           'b':2,
           'c':3}
out_dict = {key:value for key, value in my_dict.items()}
print(out_dict)    #{'a': 1, 'b': 2, 'c': 3}

Example 2: with action

Values has been updated power 2.

my_dict = {'a':1, 'b':2, 'c':3} out_dict = {key:value**2 for key, value in my_dict.items()} print(out_dict) #{'a': 1, 'b': 4, 'c': 9}

Example 3: if condition

Only even numbers

my_dict = {'a':1,
           'b':2,
           'c':3}
out_dict = {k:v**2 for k, v in my_dict.items() if v%2==0}
print(out_dict)    #{'b': 4}

Example 4:

my_list = [1,2,3,4]
out_dict = {item:item**2 for item in [1,2,3,4]}
print(out_dict)    # {1: 1, 2: 4, 3: 9, 4: 16}

If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!