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).
  • My post explains a dictionary (9).

A dictionary and other dictionary can be checked if only all the keys or both all the keys and values in:

  • the dictionary are in other dictionary with <=.
  • the dictionary and only other keys or both other keys and values are in other dictionary with <.
  • other dictionary are in the dictionary with >=.
  • other dictionary and only other keys or both other keys and values are in the dictionary with >.

*Memo:

  • Only all the values cannot be checked.
  • dict.keys() and dict.items() work for all <=, <, >= and >.
  • dict and dict.values() get error for all <=, <, >= and >.

<dict>:

v = {0:1, 2:3, 4:5}

print(v <= {0:1, 2:3, 4:5})
# TypeError: '<=' not supported between instances of 'dict' and 'dict'

print(v < {0:1, 2:3, 4:5})
# TypeError: '<' not supported between instances of 'dict' and 'dict'

print(v >= {0:1, 2:3, 4:5})
# TypeError: '>=' not supported between instances of 'dict' and 'dict'

print(v > {0:1, 2:3, 4:5})
# TypeError: '>' not supported between instances of 'dict' and 'dict'
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

v = {0:1, 2:3, 4:5}.keys()

print(v <= {0:1, 2:3, 4:5}.keys())          # True
print(v <= {4:5, 2:3, 0:1}.keys())          # True
print(v <= {0:1, 2:3, 6:7}.keys())          # False
print(v <= {0:1, 2:3, -6:-7}.keys())        # False
print(v <= {0:1, 2:3}.keys())               # False
print(v <= {0:1, 6:7}.keys())               # False
print(v <= {0:1, -6:-7}.keys())             # False
print(v <= {0:1, 2:3, 4:5, 6:7}.keys())     # True
print(v <= {0:1, 2:3, 6:7, 8:9}.keys())     # False
print(v <= {0:1, 2:3, -6:-7, -8:-9}.keys()) # False
print(v <= {}.keys())                       # False
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.keys()

print(v < {0:1, 2:3, 4:5}.keys())          # False
print(v < {4:5, 2:3, 0:1}.keys())          # False
print(v < {0:1, 2:3, 6:7}.keys())          # False
print(v < {0:1, 2:3, -6:-7}.keys())        # False
print(v < {0:1, 2:3}.keys())               # False
print(v < {0:1, 6:7}.keys())               # False
print(v < {0:1, -6:-7}.keys())             # False
print(v < {0:1, 2:3, 4:5, 6:7}.keys())     # True
print(v < {0:1, 2:3, 6:7, 8:9}.keys())     # False
print(v < {0:1, 2:3, -6:-7, -8:-9}.keys()) # False
print(v < {}.keys())                       # False
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.keys()

print(v >= {0:1, 2:3, 4:5}.keys())          # True
print(v >= {4:5, 2:3, 0:1}.keys())          # True
print(v >= {0:1, 2:3, 6:7}.keys())          # False
print(v >= {0:1, 2:3, -6:-7}.keys())        # False
print(v >= {0:1, 2:3}.keys())               # True
print(v >= {0:1, 6:7}.keys())               # False
print(v >= {0:1, -6:-7}.keys())             # False
print(v >= {0:1, 2:3, 4:5, 6:7}.keys())     # False
print(v >= {0:1, 2:3, 6:7, 8:9}.keys())     # False
print(v >= {0:1, 2:3, -6:-7, -8:-9}.keys()) # False
print(v >= {}.keys())                       # True
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.keys()

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

<dict.values()>:

v = {0:1, 2:3, 4:5}.values()

print(v <= {0:1, 2:3, 4:5}.values())
# TypeError: '<=' not supported between instances of 'dict_values' and
# 'dict_values'

print(v < {0:1, 2:3, 4:5}.values())
# TypeError: '<' not supported between instances of 'dict_values' and
# 'dict_values'

print(v >= {0:1, 2:3, 4:5}.values())
# TypeError: '>=' not supported between instances of 'dict_values' and
# 'dict_values'

print(v > {0:1, 2:3, 4:5}.values())
# TypeError: '>' not supported between instances of 'dict_values' and
# 'dict_values'
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

v = {0:1, 2:3, 4:5}.items()

print(v <= {0:1, 2:3, 4:5}.items())          # True
print(v <= {4:5, 2:3, 0:1}.items())          # True
print(v <= {0:1, 2:3, 6:7}.items())          # False
print(v <= {0:1, 2:3, -6:-7}.items())        # False
print(v <= {0:1, 2:3}.items())               # False
print(v <= {0:1, 6:7}.items())               # False
print(v <= {0:1, -6:-7}.items())             # False
print(v <= {0:1, 2:3, 4:5, 6:7}.items())     # True
print(v <= {0:1, 2:3, 6:7, 8:9}.items())     # False
print(v <= {0:1, 2:3, -6:-7, -8:-9}.items()) # False
print(v <= {}.items())                       # False
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.items()

print(v < {0:1, 2:3, 4:5}.items())          # False
print(v < {4:5, 2:3, 0:1}.items())          # False
print(v < {0:1, 2:3, 6:7}.items())          # False
print(v < {0:1, 2:3, -6:-7}.items())        # False
print(v < {0:1, 2:3}.items())               # False
print(v < {0:1, 6:7}.items())               # False
print(v < {0:1, -6:-7}.items())             # False
print(v < {0:1, 2:3, 4:5, 6:7}.items())     # True
print(v < {0:1, 2:3, 6:7, 8:9}.items())     # False
print(v < {0:1, 2:3, -6:-7, -8:-9}.items()) # False
print(v < {}.items())                       # False
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.items()

print(v >= {0:1, 2:3, 4:5}.items())          # True
print(v >= {4:5, 2:3, 0:1}.items())          # True
print(v >= {0:1, 2:3, 6:7}.items())          # False
print(v >= {0:1, 2:3, -6:-7}.items())        # False
print(v >= {0:1, 2:3}.items())               # True
print(v >= {0:1, 6:7}.items())               # False
print(v >= {0:1, -6:-7}.items())             # False
print(v >= {0:1, 2:3, 4:5, 6:7}.items())     # False
print(v >= {0:1, 2:3, 6:7, 8:9}.items())     # False
print(v >= {0:1, 2:3, -6:-7, -8:-9}.items()) # False
print(v >= {}.items())                       # True
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.items()

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

A dictionary and other dictionary can be checked if they have and don't have only their common keys or both their common keys and values with bool() and & and with not keyword and & respectively as shown below:

*Memo:

  • Only their common values cannot be checked.
  • dict.keys() and dict.items() work.
  • dict and dict.values() get error.

<dict>:

v = {0:1, 2:3, 4:5}

print(bool(v & {2:3, 6:7}))
print(not (v & {6:7, 8:9}))
# 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()

print(bool(v & {2:3, 6:7}.keys())) # True
print(bool(v & {6:7, 8:9}.keys())) # False
print(bool(v & {}.keys()))         # False

print(not (v & {2:3, 6:7}.keys())) # False
print(not (v & {6:7, 8:9}.keys())) # True
print(not (v & {}.keys()))         # True
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

v = {0:1, 2:3, 4:5}.values()

print(bool(v & {2:3, 6:7}.values()))
print(not (v & {2:3, 6:7}.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()

print(bool(v & {2:3, 6:7}.items())) # True
print(bool(v & {6:7, 8:9}.items())) # False
print(bool(v & {}.items()))         # False

print(not (v & {2:3, 6:7}.items())) # False
print(not (v & {6:7, 8:9}.items())) # True
print(not (v & {}.items()))         # True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)