DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Dictionary comprehension in Python

Buy Me a Coffee

*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}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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}}
Enter fullscreen mode Exit fullscreen mode

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}}
Enter fullscreen mode Exit fullscreen mode

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}}
Enter fullscreen mode Exit fullscreen mode

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}}
Enter fullscreen mode Exit fullscreen mode

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}}}
Enter fullscreen mode Exit fullscreen mode

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}}}
Enter fullscreen mode Exit fullscreen mode

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}}}
Enter fullscreen mode Exit fullscreen mode

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}}}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)