DEV Community

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

Posted on

Comprehension in Python (2)

Buy Me a Coffee

*Memos:

  • My post explains a list comprehension, tuple comprehension and set comprehension.

<Dictionary(Dict) Comprehension>

1D dictionary:

v = {x:x**2 for x in range(6)} print(v) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25} 
Enter fullscreen mode Exit fullscreen mode

*The below is without a dictionary comprehension.

v = {} for x in range(6): v.update({x:x**2}) print(v) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25} 
Enter fullscreen mode Exit fullscreen mode

2D dictionary:

*Memos:

  • A key can have a tuple, but not a list, set and dictionary.
sample = ((0, 1, 2), (3, 4, 5)) v = {x: {y:y**2 for y in x} for x in sample} print(v) # {(0, 1, 2): {0: 0, 1: 1, 2: 4}, (3, 4, 5): {3: 9, 4: 16, 5: 25}} 
Enter fullscreen mode Exit fullscreen mode

*The below is without a dictionary comprehension.

sample = ((0, 1, 2), (3, 4, 5)) v = {} for x in sample: v.update({x:{}}) for y in x: v[x].update({y:y**2}) print(v) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16} # {(0, 1, 2): {0: 0, 1: 1, 2: 4}, (3, 4, 5): {3: 9, 4: 16, 5: 25}} 
Enter fullscreen mode Exit fullscreen mode

<Generator Comprehension>

*Memos:

  • Basically, generator comprehension needs () but it can be passed to a function without ().

1D generator:

v = (x**2 for x in range(6)) print(v) # <generator object <genexpr> at 0x000001C1CBA37440>  print(next(v), next(v), next(v), next(v), next(v), next(v)) # 0 1 4 9 16 25 
Enter fullscreen mode Exit fullscreen mode

*The below is without a generator comprehension.

def func(): for x in range(6): yield x**2 v = func() print(v) # <generator object func at 0x000001C1CB737A00>  print(next(v), next(v), next(v), next(v), next(v), next(v)) # 0 1 4 9 16 25 
Enter fullscreen mode Exit fullscreen mode
print(x**2 for x in range(6)) # No `()` # <generator object <genexpr> at 0x000001C1CBA341E0> 
Enter fullscreen mode Exit fullscreen mode
v = x**2 for x in range(6) # No `()` # SyntaxError: invalid syntax 
Enter fullscreen mode Exit fullscreen mode

2D generator:

sample = [[0, 1, 2], [3, 4, 5]] v1 = ((y**2 for y in x) for x in sample) v2 = next(v1) v3 = next(v1) print(v1) # <generator object <genexpr> at 0x000001C1CBA698A0>  print(v2, v3) # <generator object <genexpr>.<genexpr> at 0x000001C1CB7BD8A0> # <generator object <genexpr>.<genexpr> at 0x000001C1CB8D3510>  print(next(v2), next(v2), next(v2), next(v3), next(v3), next(v3)) # 0 1 4 9 16 25 
Enter fullscreen mode Exit fullscreen mode

*The below is without a generator comprehension.

def func(): def func(x): for y in x: yield y**2 sample = [[0, 1, 2], [3, 4, 5]] for x in sample: yield func(x) v1 = func() v2 = next(v1) v3 = next(v1) print(v1) # <generator object func at 0x000001C1CB9F44A0>  print(v2, v3) # <generator object func.<locals>.func at 0x000001C1CBA698A0> # <generator object func.<locals>.func at 0x000001C1CB7BD8A0>  print(next(v2), next(v2), next(v2), next(v3), next(v3), next(v3)) # 0 1 4 9 16 25 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)