python dictionary comprehensions code example
Example 1: python set and dictionary comprehensions
simple_dict = {
'a': 1,
'b': 2
}
my_dict = {key: value**2 for key,value in simple_dict.items()}
print(my_dict)
Example 2: create dictionary comprehension python
{key:value for key in iterable}
Example 3: python dictionary comprehension
dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
double_dict1 = {k:v*2 for (k,v) in dict1.items()}
Example 4: dict comprehension python
fruits = ["apple", "banana", "cherry"]
print({f: len(f) for f in fruits})
{'apple': 5, 'banana': 6, 'cherry': 6}
Example 5: dictionary comprehension python
print({i:j for i,j in zip(txt_list,num) if i!="All"})