DEV Community

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

Posted on

Dictionary in Python (4)

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

A dictionary and other dictionary can be checked if all the keys and/or values in them are and aren't the same with == and != respectively as shown below:

<dict>:

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

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

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

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

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 <=.
  • other dictionary are in the dictionary with >=.
  • the dictionary and other elements are in other dictionary with <.
  • other dictionary and other elements are in the dictionary with >.

*Memo:

<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 >= {0:1, 2:3, 4:5}.keys()) # True
print(v < {0:1, 2:3, 4:5}.keys())  # False
print(v > {0:1, 2:3, 4:5}.keys())  # False
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 >= {0:1, 2:3, 4:5}.items()) # True
print(v < {0:1, 2:3, 4:5}.items())  # False
print(v > {0:1, 2:3, 4:5}.items())  # False
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(not (v & {2:3, 6:7}.keys())) # False
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(not (v & {2:3, 6:7}.items())) # False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)