DEV Community

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

Posted on • Edited on

Comprehension in Python (2)

Buy Me a Coffee

*Memo:

  • My post explains a list and tuple comprehension.
  • My post explains a generator comprehension.
  • My post explains a set and the set with copy.
  • My post explains a dictionary (1).

<Set Comprehension>:

1D set:

sample = {0, 1, 2, 3, 4, 5, 6, 7}

A = {x**2 for x in sample}

print(A)
# {0, 1, 4, 36, 9, 16, 49, 25}
Enter fullscreen mode Exit fullscreen mode

The below is without a set comprehension:

sample = {0, 1, 2, 3, 4, 5, 6, 7}

A = set()

for x in sample:
    A.add(x**2)

print(A)
# {0, 1, 4, 36, 9, 16, 49, 25}
Enter fullscreen mode Exit fullscreen mode

2D set:

sample = {frozenset({0, 1, 2, 3}), frozenset({4, 5, 6, 7})}

A = {frozenset(y**2 for y in x) for x in sample}

print(A)
# {frozenset({16, 25, 36, 49}), frozenset({0, 1, 4, 9})}
Enter fullscreen mode Exit fullscreen mode

The below is without a set comprehension:

sample = {frozenset({0, 1, 2, 3}), frozenset({4, 5, 6, 7})}

A = set()

for x in sample:
    def func(x):
        for y in x:
            yield y**2
    A.add(frozenset(func(x)))

print(A)
# {frozenset({16, 25, 36, 49}), frozenset({0, 1, 4, 9})}
Enter fullscreen mode Exit fullscreen mode

3D set:

sample = {frozenset({frozenset({0, 1}), frozenset({2, 3})}),
          frozenset({frozenset({4, 5}), frozenset({6, 7})})}

A = {frozenset(frozenset(z**2 for z in y) for y in x) for x in sample}

print(A)
# {frozenset({frozenset({16, 25}), frozenset({49, 36})}),
#  frozenset({frozenset({9, 4}), frozenset({0, 1})})}
Enter fullscreen mode Exit fullscreen mode

The below is without a set comprehension:

sample = {frozenset({frozenset({0, 1}), frozenset({2, 3})}),
          frozenset({frozenset({4, 5}), frozenset({6, 7})})}
A = set()

for x in sample:
    def func(x):
        for z, y in x:
            yield frozenset({z**2, y**2})
    A.add(frozenset(func(x)))

print(A)
# {frozenset({frozenset({16, 25}), frozenset({49, 36})}),
#  frozenset({frozenset({9, 4}), frozenset({0, 1})})}
Enter fullscreen mode Exit fullscreen mode

<Dictionary(Dict) Comprehension>:

*Memo:

  • A key can have a tuple but not a dictionary, list and set.

1D dictionary:

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

2D dictionary:

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

3D dictionary:

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

Top comments (0)