DEV Community

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

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

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

*Memo:

  • For dict.values(), == and != always return False and True respectively.
  • == and != can also check if types of values are and aren't the same respectively:
    • dict.keys() and a set or frozenset and vice versa are True even though their types are different.
    • dict.items() and a set(tuple) or frozenset(tuple) and vice versa are True even though their types are different.

<dict>:

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

print(v == {0:1, 2:3, 4:5})          # True
print(v == {4:5, 2:3, 0:1})          # True
print(v == {0:1, 2:3, 6:7})          # False
print(v == {0:1, 2:3, -6:-7})        # False
print(v == {0:1, 2:3})               # False
print(v == {0:1, 6:7})               # False
print(v == {0:1, -6:-7})             # 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 == {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 != {4:5, 2:3, 0:1})          # False
print(v != {0:1, 2:3, 6:7})          # True
print(v != {0:1, 2:3, -6:-7})        # True
print(v != {0:1, 2:3})               # True
print(v != {0:1, 6:7})               # True
print(v != {0:1, -6:-7})             # 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 != {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)
# dict_keys([0, 2, 4])

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())     # 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())                       # False

            # list
print(v == [0, 2, 4])            # False

             # set
print(v == {0, 2, 4})            # True
print(v == frozenset([0, 2, 4])) # True
               # frozenset
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.keys()

print(v)
# dict_keys([0, 2, 4])

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())          # True
print(v != {0:1, 2:3, -6:-7}.keys())        # True
print(v != {0:1, 2:3}.keys())               # True
print(v != {0:1, 6:7}.keys())               # True
print(v != {0:1, -6:-7}.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 != {0:1, 2:3, -6:-7, -8:-9}.keys()) # True
print(v != {}.keys())                       # True

            # list
print(v != [0, 2, 4])            # True

             # set
print(v != {0, 2, 4})            # False
print(v != frozenset([0, 2, 4])) # False
               # frozenset
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

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

print(v)
# dict_values([1, 3, 5])

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

            # list
print(v == [1, 3, 5])            # False

             # set
print(v == {1, 3, 5})            # False
print(v == frozenset([1, 3, 5])) # False
               # frozenset
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.values()

print(v)
# dict_values([1, 3, 5])

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

            # list
print(v != [1, 3, 5])            # True

             # set
print(v != {1, 3, 5})            # True
print(v != frozenset([1, 3, 5])) # True
               # frozenset
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

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

print(v)
# dict_items([(0, 1), (2, 3), (4, 5)])

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())     # 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())                       # False

                # list(tuple)
print(v == [(0, 1), (2, 3), (4, 5)])            # False

                 # set(tuple)
print(v == {(0, 1), (2, 3), (4, 5)})            # True
print(v == frozenset([(0, 1), (2, 3), (4, 5)])) # True
                   # frozenset(tuple)
Enter fullscreen mode Exit fullscreen mode
v = {0:1, 2:3, 4:5}.items()

print(v)
# dict_items([(0, 1), (2, 3), (4, 5)])

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())          # True
print(v != {0:1, 2:3, -6:-7}.items())        # True
print(v != {0:1, 2:3}.items())               # True
print(v != {0:1, 6:7}.items())               # True
print(v != {0:1, -6:-7}.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 != {0:1, 2:3, -6:-7, -8:-9}.items()) # True
print(v != {}.items())                       # True

                # list(tuple)
print(v != [(0, 1), (2, 3), (4, 5)])            # True

                 # set(tuple)
print(v != {(0, 1), (2, 3), (4, 5)})            # False
print(v != frozenset([(0, 1), (2, 3), (4, 5)])) # False
                   # frozenset(tuple)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)