payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Comprehensions and Ternary Expressions   in Category: Python   by amit

🕙 Posted on 2023-08-27 at 08:16:10     Read in Hindi ...


List, Dictionary & Set Compre­hens­ions

    You have heard of ?: (ternary operator) in other programming languages, such as PHP and JavaScript. However, in Python the ternary operator is of lowest priority among all other operations, and an example of it is shown below. But, List Compre­hens­ions & Dictionary Compre­hens­ions are frequently used in Python, and are very important. The ternary operator and List/Dictionary/Set Compre­hens­ions are single-line expressions, and they are used to assign such an expression to a variable name. In this way, programmers have to write less, do more.

Ternary operator

Example 1:

x, y = 30, 20
result = x if x < y else y
print( result )   # Outputs 20

    The above example when written in if else block, you can see that above three lines of codes will be written in six lines, also with indentation. In programming languages, output of an expression is generally assigned to a variable name, so that it can be further used in background or back-end processes. Displaying result using the print() built-in function or using other modules are rare and end-result of whole program.

Example 2:

x, y = 30, 20
if x < y:
    result = x
else:
    result = y
print( result )   # Outputs 20

Comprehensions

    You have already seen various examples of for loop. Here you should note that all List, Dictionary & Set Compre­hens­ions are short forms of for loop, and therefore, they can also be written in for loop block of code. But, a for loop block can contain multiple statements (lines of codes), and therefore a complex (extensive) for loop block of code cannot be written in List/Dictionary Compre­hens­ions. Same analogy is true about if else block of codes and ternary expressions (explained above).

    When your data contains large amount of floats (that is, items/elements in a list are floats), then storing and processing those floats can take large memory (RAM) and calculating power (CPU). However, these lags are negligible, when there are few items (as shown in following example). Think your data to be of temper­ature values in celcius (floats) for every hours of a year. Then items in the list will be 365 × 24 = 8760. Many weather forecast organiz­ations evaluate these kind of data for every city in the world. Handling such BIG DATA can be challenge for programmers!

List Comprehensions

    Compre­hens­ions are for mutable sequence data-types, that is list, dictionary and set. Syntax of LIST Compre­hens­ion is as follows:

variable_name = [expression for item in iterable]

Example 3: In the following example, temps is original list data-type (iterable), also inside a pair of square brackets, so that the for loop can fetch each item/element from it. temp is individual item/element of list data-type, which is further divided by 10 (or other operations can be done in expression) and thereafter, the output of the expression, temp / 10 is stored in another list literal (or variable name), that is,my_temps.

temps = [351, 344, 340, 445]   # don't use decimals to save data-storage
my_temps = [temp / 10 for temp in temps]   # list compre­hens­ion
print(my_temps, type(my_temps))

[35.1, 34.4, 34.0, 44.5] <class 'list'>

Example 4:

temps = [351, 344, 340, 445]
new_temps = []
for temp in temps:
    new_temps.append(temp / 10)
print(new_temps, type(new_temps))   # Outputs same as above

[35.1, 34.4, 34.0, 44.5] <class 'list'>

    Caution: You should note that a variable name defined inside the list compre­hens­ion syntax (for example, my_var inside the [my_var for my_var in my_list] expression) is not accessible from outside of it. That is, it is local to the list compre­hens­ion. However, a variable name (for example, item in the following block of codes,) defined inside the for loop is available and accessible outside (below) the loop.

for item in [1,2,3,4,5]:
    print( item )   # Inside the loop
print( item )   # Outside the loop, outputs the last item of the iterable.

1
2
3
4
5
5

    Therefore, you should be careful, while using these types of variables declared inside an expression or a block of codes. The variable scope will be explained in later pages. Syntax of LIST Compre­hens­ion (extended) is as follows:

variable_name = [expression for item in iterable if item condition literal]

Example 5:In the following example, temps is original list, of integer items in it. temp is each element of list, temps on which a condition is checked by >= (a comparison operator). When the condition on an item, is evaluated as False, the respective item of original list is not passed to the expression of list compre­hens­ion. Thereafter, the conditional expression is further divided by 10. And, finally temp is appended to new variable name, temp_result.

temps = [351, 344, -999, 340, 445]   # some invalid data in list
temp_result = [temp / 10 for temp in temps if temp >= -273.0]
print(temp_result)

[35.1, 34.4, 34.0, 44.5]

    The List Compre­hens­ion has many parts, before it is assigned with a variable name or evaluated directly inside print() function. When the condition is before the for loop, then each item of original list will be compared with condition, first. When the condition is evaluated False, the output of else block, will be passed to the expression, otherwise original item of the list will be passed to the expression of list compre­hens­ion.

variable_name = [expression if item condition literal else alternate_item for item in iterable]

Example 6:

temps = [351, 344, -999, 340, 445]   # some negative data in list
print( [temp/10 if temp > 0 else 0 for temp in temps] )

[35.1, 34.4, 0, 34.0, 44.5]

    In example 5, you can see that the negative value (of third item) is not in the resulting list (final output), whereas in example 6, 0 (value of third item, after the else is passed) replaces the negative item of original list.

Example 7: In the following example, you can create a list of even numbers from the original list, or a list of odd numbers, if you replace == (equal to) operator to != (not equal to) operator. The concept of list compre­hens­ion is explained above.

my_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list = [num for num in my_numbers if num % 2 == 0]
print( my_list )   # Outputs [2, 4, 6, 8, 10]

Example 8:

my_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list = []         # empty list
for num in my_numbers:
    if num % 2 == 0:
        my_list.append(num)
print( my_list )   # Outputs [2, 4, 6, 8, 10]

List Compre­hens­ion on String

Example 9: This simple list compre­hens­ion is self-explanatory. You can write the traditional for loop of following codes, yourself.

my_str = "Hello World"
items = [letter for letter in my_str]
print(items)

['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

More complex List Compre­hens­ion

Example 10: You can use range() built-in function instead of [1, 2, 3, 4, 5, 6] (a list literal) in following example. The outputs of all three examples described below are same (shown at bottom of Example 12). In following three examples, for every letter (first item in nested tuple) in string literal, 'abcdef', each number (or you can place other data-type literals) in second list literal, is assigned as second item of nested tuple.

my_list = [(letter, num) for letter in 'abcdef' for num in [1, 2, 3, 4, 5, 6]]
print( my_list )   # Output is shown below:

Example 11: range() built-in function is explained in next page.

my_list = [(letter, num) for letter in 'abcdef' for num in range(6)]
print( my_list )   # Output is shown below:

Example 12:

my_list = []
for letter in 'abcdef':
    for num in [1, 2, 3, 4, 5, 6]:
        my_list.append((letter, num))
print( my_list )   # Output is shown below:

[('a', 1), ('a', 2), ('a', 3), ('a', 4), ('a', 5), ('a', 6), ('b', 1), ('b', 2), ('b', 3), ('b', 4), ('b', 5), ('b', 6), ('c', 1), ('c', 2), ('c', 3), ('c', 4), ('c', 5), ('c', 6), ('d', 1), ('d', 2), ('d', 3), ('d', 4), ('d', 5), ('d', 6), ('e', 1), ('e', 2), ('e', 3), ('e', 4), ('e', 5), ('e', 6), ('f', 1), ('f', 2), ('f', 3), ('f', 4), ('f', 5), ('f', 6)]

Dictionary Compre­hens­ions

Example 13: Though following example is lengthy, it is shown herein below that a dictionary compre­hens­ion can be created with the zip() built-in function, or a list/tuple of nested tuples (in which first item of each nested tuple is the key, and second item of that nested tuple is the value of respective key). You should remember that a key name can be only immutable data-type, which also should be hashable. To know how to create a dictionary, you should visit previous page.

roman_num = ['i', 'ii', 'iii', 'iv', 'v', 'vi']
english_num = ['one', 'two', 'three', 'four', 'five', 'six']

print( zip( roman_num, english_num ) )   # Outputs <zip object at 0x000001959E2427C0> (a zip object)
print( list(zip( roman_num, english_num )) )       # Outputs a list of tuples
print( tuple(zip( roman_num, english_num )) )   # Outputs a tuple of tuples
print( dict(zip( roman_num, english_num )) )      # Outputs a dictionary with respective items of roman_num and english_num as key:value pair.

my_dict = {}   # empty dictionary
for rom_num, eng_num in zip( roman_num, english_num ):
    my_dict[rom_num] = eng_num
print( my_dict )       # Outputs a dictionary with rom_num:eng_num as key:value pair. You should not use these local variables in following dictionary compre­hens­ion, otherwise you will get ERROR, or unexpected result!

new_dict = { r_num: e_num for r_num, e_num in zip( roman_num, english_num ) }
print( new_dict )       # Outputs a dictionary with r_num:e_num as key:value pair.

<zip object at 0x000001959E2427C0>
[('i', 'one'), ('ii', 'two'), ('iii', 'three'), ('iv', 'four'), ('v', 'five'), ('vi', 'six')]
(('i', 'one'), ('ii', 'two'), ('iii', 'three'), ('iv', 'four'), ('v', 'five'), ('vi', 'six'))
{'i': 'one', 'ii': 'two', 'iii': 'three', 'iv': 'four', 'v': 'five', 'vi': 'six'}
{'i': 'one', 'ii': 'two', 'iii': 'three', 'iv': 'four', 'v': 'five', 'vi': 'six'}
{'i': 'one', 'ii': 'two', 'iii': 'three', 'iv': 'four', 'v': 'five', 'vi': 'six'}

Example 14: In the following example, you can use conditional expression inside the dictionary compre­hens­ion, same as explained above for the list compre­hens­ions. Items which are evaluated as False will not be added to the output of dictionary compre­hens­ion literal.

roman_num = ['i', 'ii', 'iii', 'iv', 'v', 'vi']
english_num = ['one', 'two', 'three', 'four', 'five', 'six']

new_dict = {r_num: e_num for r_num, e_num in zip(roman_num, english_num) if r_num != 'iv'}
print(new_dict)

{'i': 'one', 'ii': 'two', 'iii': 'three', 'v': 'five', 'vi': 'six'}

Example 15: In the following example, you can see the same kind of syntax (which is explained above for extended list compre­hens­ions). When any item (key:value pair) of the dictionary literal doesn't qualify the if condition, then else block of codes will be passed as an item of dictionary compre­hens­ion. See the third item in output descibed below of following example.

roman_num = ['i', 'ii', 'iii', 'iv', 'v', 'vi']
english_num = ['one', 'two', 'three', 'four', 'five', 'six']

new_dict = {r_num: e_num if r_num != 'iv' else (4,'xyz') for r_num, e_num in zip(roman_num, english_num)}
print(new_dict)

{'i': 'one', 'ii': 'two', 'iii': 'three', 'iv': (4, 'xyz'), 'v': 'five', 'vi': 'six'}

Set Comprehensions

Example 16: You can convert a list or tuple data-type literal to set data-type by using set() built-in function. For example, set( [1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6] ) (type-casting a list into set) or set( (1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6) ) (type-casting a tuple into set) or {1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6} (a simple set), all three will output or return {1, 2, 3, 4, 5, 6} (set only contains unique items).

my_set = {1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6}
print( my_set )   # Outputs {1, 2, 3, 4, 5, 6}

new_set = set()   # do not use {} curly braces, which will create a dictionary
for num in my_set:
    new_set.add( num )
print( new_set )   # Outputs {1, 2, 3, 4, 5, 6}

Example 17: Do not use num (local variable) defined in above for loop, when using examples in continuity. Otherwise, you may be get ERROR or unexpected result. Remember, an ERROR or BUG occurs in programs, when the programmer (human being) doesn't take care of these silly mistakes!! A BUG is not an error, but an unexpected behaviour which may not be displayed by the Python interpreter.

my_set = {1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6}
new_set = {num for num in my_set}
print( new_set )   # Outputs {1, 2, 3, 4, 5, 6}


Leave a Comment: