list and dictionary comprehension in python code example
Example 1: dictionary comprehension in python
sample_dict = {
'a': 1,
'b': 2,
'c': 3,
'd': 4,
'e': 5
}
square_dict = {key:value**2 for key, value in sample_dict.items()}
print(square_dict)
square_dict_even = {key:value**2 for key, value in sample_dict.items() if value % 2 == 0}
print(square_dict_even)
square_without_dict = {num:num**2 for num in range(11)}
print(square_without_dict)
Example 2: dictionary comprehension python
print({i:j for i,j in zip(txt_list,num) if i!="All"})