
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 Comprehensions
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 Comprehensions & Dictionary Comprehensions are frequently used in Python, and are very important. The ternary operator and List/
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, 20if
x < y:
result = xelse
:
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 Comprehensions 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/if else
block of codes and ternary expressions (explained above).
When your data contains large amount of floats (that is, items/
List Comprehensions
Comprehensions are for mutable sequence data-types, that is list, dictionary and set. Syntax of LIST Comprehension 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/
temps = [351, 344, 340, 445] # don't use decimals to save data-storage
my_temps = [temp / 10 for
temp in
temps] # list comprehension
print(my_temps, type(my_temps))
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
Caution
: You should note that a variable name defined inside the list comprehension 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 comprehension. 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.
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 Comprehension (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 comprehension. 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)
The List Comprehension 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 comprehension.
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] )
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 comprehension 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 listfor
num in
my_numbers:
if
num % 2 == 0:
my_list.append(num)
print( my_list ) # Outputs [2, 4, 6, 8, 10]
List Comprehension on String
Example 9: This simple list comprehension 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)
More complex List Comprehension
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:
Dictionary Comprehensions
Example 13: Though following example is lengthy, it is shown herein below that a dictionary comprehension can be created with the zip()
built-in function, or a list/
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 0x000001959
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 dictionaryfor
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:
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:
[('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 comprehension, same as explained above for the list comprehensions. Items which are evaluated as False will not be added to the output of dictionary comprehension 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)
Example 15: In the following example, you can see the same kind of syntax (which is explained above for extended list comprehensions). 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 comprehension. 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)
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 dictionaryfor
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:

Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤

Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇