*Memo:
- My post explains a list comprehension.
- My post explains a tuple comprehension.
- My post explains a set comprehension.
- My post explains a frozenset comprehension.
- My post explains a generator comprehension.
- My post explains a dictionary (1).
A comprehension is the concise expression to create an iterable and there are a list, tuple, set, frozenset, dictionary(dict) and generator comprehension:
<Dictionary(Dict) comprehension>:
*Memo:
- A key can have a tuple but not a dictionary, list and set.
1D dictionary (1):
sample = [0, 1, 2, 3, 4, 5, 6, 7]
v = {x:x**2 for x in sample}
print(v)
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}
The below is without a dictionary comprehension:
sample = [0, 1, 2, 3, 4, 5, 6, 7]
v = {}
for x in sample:
v.update({x:x**2})
print(v)
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}
1D dictionary (2):
sample = [0, 1, 2, 3, 4, 5, 6, 7]
v = {i:x**2 for i, x in enumerate(sample)}
print(v)
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}
The below is without a dictionary comprehension:
sample = [0, 1, 2, 3, 4, 5, 6, 7]
v = {}
for i, x in enumerate(sample):
v.update({i:x**2})
print(v)
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}
2D dictionary (1):
sample = ((0, 1, 2, 3), (4, 5, 6, 7))
v = {x: {y:y**2 for y in x} for x in sample}
print(v)
# {(0, 1, 2, 3): {0: 0, 1: 1, 2: 4, 3: 9},
# (4, 5, 6, 7): {4: 16, 5: 25, 6: 36, 7: 49}}
The below is without a dictionary comprehension:
sample = ((0, 1, 2, 3), (4, 5, 6, 7))
v = {}
for x in sample:
v.update({x:{}})
for y in x:
v[x].update({y:y**2})
print(v)
# {(0, 1, 2, 3): {0: 0, 1: 1, 2: 4, 3: 9},
# (4, 5, 6, 7): {4: 16, 5: 25, 6: 36, 7: 49}}
2D dictionary (2):
sample = ((0, 1, 2, 3), (4, 5, 6, 7))
v = {i: {j:y**2 for j, y in enumerate(x)} for i, x in enumerate(sample)}
print(v)
# {0: {0: 0, 1: 1, 2: 4, 3: 9},
# 1: {0: 16, 1: 25, 2: 36, 3: 49}}
The below is without a dictionary comprehension:
sample = ((0, 1, 2, 3), (4, 5, 6, 7))
v = {}
for i, x in enumerate(sample):
v.update({i:{}})
for j, y in enumerate(x):
v[i].update({j:y**2})
print(v)
# {0: {0: 0, 1: 1, 2: 4, 3: 9},
# 1: {0: 16, 1: 25, 2: 36, 3: 49}}
3D dictionary (1):
sample = (((0, 1), (2, 3)), ((4, 5), (6, 7)))
v = {x: {y: {z:z**2 for z in y} for y in x} for x in sample}
print(v)
# {((0, 1), (2, 3)): {(0, 1): {0: 0, 1: 1}, (2, 3): {2: 4, 3: 9}},
# ((4, 5), (6, 7)): {(4, 5): {4: 16, 5: 25}, (6, 7): {6: 36, 7: 49}}}
The below is without a dictionary comprehension:
sample = (((0, 1), (2, 3)), ((4, 5), (6, 7)))
v = {}
for x in sample:
v.update({x:{}})
for y in x:
v[x].update({y:{}})
for z in y:
v[x][y].update({z:z**2})
print(v)
# {((0, 1), (2, 3)): {(0, 1): {0: 0, 1: 1}, (2, 3): {2: 4, 3: 9}},
# ((4, 5), (6, 7)): {(4, 5): {4: 16, 5: 25}, (6, 7): {6: 36, 7: 49}}}
3D dictionary (2):
sample = (((0, 1), (2, 3)), ((4, 5), (6, 7)))
e = enumerate
v = {i: {j: {k:z**2 for k, z in e(y)} for j, y in e(x)} for i, x in e(sample)}
print(v)
# {0: {0: {0: 0, 1: 1}, 1: {0: 4, 1: 9}},
# 1: {0: {0: 16, 1: 25}, 1: {0: 36, 1: 49}}}
The below is without a dictionary comprehension:
sample = (((0, 1), (2, 3)), ((4, 5), (6, 7)))
v = {}
for i, x in enumerate(sample):
v.update({i:{}})
for j, y in enumerate(x):
v[i].update({j:{}})
for k, z in enumerate(y):
v[i][j].update({k:z**2})
print(v)
# {0: {0: {0: 0, 1: 1}, 1: {0: 4, 1: 9}},
# 1: {0: {0: 16, 1: 25}, 1: {0: 36, 1: 49}}}
Top comments (0)