DEV Community

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

Posted on

Dictionary in Python (5)

Buy Me a Coffee

*Memo:

  • My post explains a dictionary (1).
  • My post explains a dictionary (2).
  • My post explains a dictionary (3).
  • My post explains a dictionary (4).
  • My post explains a dictionary (6).
  • My post explains a dictionary (7).
  • My post explains a dictionary (8).

A dictionary cannot be enlarged with * and a number as shown below:

<dict>:

v = {'name':'John', 'age':36} * 3
# TypeError: unsupported operand type(s) for *: 'dict' and 'int'
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

v = {'name':'John', 'age':36}.keys() * 3
# TypeError: unsupported operand type(s) for *: 'dict_keys' and 'int'
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

v = {'name':'John', 'age':36}.values() * 3
# TypeError: unsupported operand type(s) for *: 'dict_values' and 'int'
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

v = {'name':'John', 'age':36}.items() * 3
# TypeError: unsupported operand type(s) for *: 'dict_items' and 'int'
Enter fullscreen mode Exit fullscreen mode

A dictionary and other dictionaries cannot be concatenated with + as shown below:

<dict>:

v = {0:1, 2:3, 4:5} + {6:7, 8:9} + {10:11, 12:13, 14:15, 16:17}
# TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

v = {0:1, 2:3, 4:5}.keys() + {6:7, 8:9}.keys() \
    + {10:11, 12:13, 14:15, 16:17}.keys()
# TypeError: unsupported operand type(s) for +: 'dict_keys' and 'dict_keys'
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

v = {0:1, 2:3, 4:5}.values() + {6:7, 8:9}.values() \
    + {10:11, 12:13, 14:15, 16:17}.values()
# TypeError: unsupported operand type(s) for +: 'dict_values' and 'dict_values'
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

v = {0:1, 2:3, 4:5}.items() + {6:7, 8:9}.items() \
    + {10:11, 12:13, 14:15, 16:17}.items()
# TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'
Enter fullscreen mode Exit fullscreen mode

A dictionary and other dictionaries can return only all the keys or both all the keys and values in them with '|' (Union: A ∪ B) as shown below:

*Memo:

<dict>:

v = {0:1, 2:3, 4:5} | {6:7, 8:9} | {10:11, 12:13, 14:15, 16:17}

print(v)
# {0: 1, 2: 3, 4: 5, 6: 7, 8: 9, 10: 11, 12: 13, 14: 15, 16: 17}
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

v = {0:1, 2:3, 4:5}.keys() | {6:7, 8:9}.keys() \
    | {10:11, 12:13, 14:15, 16:17}.keys()

print(v)
# {0, 2, 4, 6, 8, 10, 12, 14, 16}
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

v = {0:1, 2:3, 4:5}.values() | {6:7, 8:9}.values() \
    | {10:11, 12:13, 14:15, 16:17}.values()
# TypeError: unsupported operand type(s) for |: 'dict_values' and 'dict_values'
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

v = {0:1, 2:3, 4:5}.items() | {6:7, 8:9}.items() \
    | {10:11, 12:13, 14:15, 16:17}.items()

print(v)
# {(0, 1), (10, 11), (12, 13), (6, 7), (8, 9),
#  (2, 3), (4, 5), (14, 15), (16, 17)}
Enter fullscreen mode Exit fullscreen mode

A dictionary and other dictionaries cannot return their common keys and/or values with '&' (Intersection: A ∩ B) as shown below:

*Memo:

  • dict.keys() and dict.items() work but an empty set is returned.
  • dict and dict.values() get error.

<dict>:

v = {0:1, 2:3, 4:5} & {6:7, 8:9} & {10:11, 12:13, 14:15, 16:17}
# TypeError: unsupported operand type(s) for &: 'dict' and 'dict'
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

v = {0:1, 2:3, 4:5}.keys() & {6:7, 8:9}.keys() \
    & {10:11, 12:13, 14:15, 16:17}.keys()

print(v)
# set()
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

v = {0:1, 2:3, 4:5}.values() & {6:7, 8:9}.values() \
    & {10:11, 12:13, 14:15, 16:17}.values()

print(v)
# TypeError: unsupported operand type(s) for &: 'dict_values' and 'dict_values'
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

v = {0:1, 2:3, 4:5}.items() & {6:7, 8:9}.items() \
    & {10:11, 12:13, 14:15, 16:17}.items()

print(v)
# set()
Enter fullscreen mode Exit fullscreen mode

A dictionary and other dictionaries can return only the keys or both the keys and values in the dictionary which aren't in other dictionaries with '-' (Difference: A - B) as shown below:

*Memo:

  • Only the values cannot be returned.
  • dict.keys() and dict.items() work.
  • dict and dict.values() get error.

<dict>:

v = {0:1, 2:3, 4:5} - {6:7, 8:9} - {10:11, 12:13, 14:15, 16:17}
# TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

v = {0:1, 2:3, 4:5}.keys() - {6:7, 8:9}.keys() \
    - {10:11, 12:13, 14:15, 16:17}.keys()

print(v)
# {0, 2, 4}
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

v = {0:1, 2:3, 4:5}.values() - {6:7, 8:9}.values() \
    - {10:11, 12:13, 14:15, 16:17}.values()
# TypeError: unsupported operand type(s) for -: 'dict_values' and 'dict_values'
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

v = {0:1, 2:3, 4:5}.items() - {6:7, 8:9}.items() \
    - {10:11, 12:13, 14:15, 16:17}.items()

print(v)
# {(0, 1), (4, 5), (2, 3)}
Enter fullscreen mode Exit fullscreen mode

A dictionary and other dictionary can return only the keys or both the keys and values in the dictionary but not in other dictionary or not in the dictionary but in other dictionary with '^' (Symmetric Difference: A Δ B) as shown below:

*Memo:

  • Only the values cannot be returned.
  • dict.keys() and dict.items() work.
  • dict and dict.values() get error.

<dict>:

print({0:1, 2:3, 4:5, 6:7} ^ {0:1, 4:5, 8:9})
# TypeError: unsupported operand type(s) for ^: 'dict' and 'dict'
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

print({0:1, 2:3, 4:5, 6:7}.keys() ^ {0:1, 4:5, 8:9}.keys())
# {8, 2, 6}
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

print({0:1, 2:3, 4:5, 6:7}.values() ^ {0:1, 4:5, 8:9}.values())
# TypeError: unsupported operand type(s) for ^: 'dict_values' and 'dict_values'
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

print({0:1, 2:3, 4:5, 6:7}.items() ^ {0:1, 4:5, 8:9}.items())
# {(2, 3), (6, 7), (8, 9)}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)