DEV Community

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

Posted on

Dictionary in Python (3)

Buy Me a Coffee

*Memo:

  • My post explains a dictionary (1).
  • My post explains a dictionary (2).
  • My post explains a dictionary (4).
  • 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 non-empty dictionary and empty dictionary are:

  • True and False, checking them with bool() respectively.
  • False and True, inverting their truth values with not keyword respectively.

<dict>:

print(bool({0:0}))   # dict
print(bool({():{}})) # dict(Empty tuple:Empty dict)
# True

print(bool({})) # Empty dict
# False
Enter fullscreen mode Exit fullscreen mode
print(not {0:0})   # dict
print(not {():{}}) # dict(Empty tuple:Empty dict)
# False

print(not {}) # Empty dict
# True
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

print(bool({0:0}.keys()))   # dict.keys()
print(bool({():{}}.keys())) # dict(Empty tuple:Empty dict).keys()
# True

print(bool({}.keys())) # Empty dict.keys()
# False
Enter fullscreen mode Exit fullscreen mode
print(not {0:0}.keys())   # dict.keys()
print(not {():{}}.keys()) # dict(Empty tuple:Empty dict).keys()
# False

print(not {}.keys()) # Empty dict.keys()
# True
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

print(bool({0:0}.values()))   # dict.values()
print(bool({():{}}.values())) # dict(Empty tuple:Empty dict).values()
# True

print(bool({}.values())) # Empty dict.values()
# False
Enter fullscreen mode Exit fullscreen mode
print(not {0:0}.values())   # dict.values()
print(not {():{}}.values()) # dict(Empty tuple:Empty dict).values()
# False

print(not {}.values()) # Empty dict.values()
# True
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

print(bool({0:0}.items()))   # dict.items()
print(bool({():{}}.items())) # dict(Empty tuple:Empty dict).items()
# True

print(bool({}.items())) # Empty dict.items()
# False
Enter fullscreen mode Exit fullscreen mode
print(not {0:0}.items())   # dict.items()
print(not {():{}}.items()) # dict(Empty tuple:Empty dict).items()
# False

print(not {}.items()) # Empty dict.items()
# True
Enter fullscreen mode Exit fullscreen mode

A dictionary can be checked if a specific key and/or value are and aren't in the dictionary with in keyword and with not and in keyword respectively as shown below:

<dict & dict.keys()>:

v = {'A':'B', 'C':{'D':'E'}}
v = {'A':'B', 'C':{'D':'E'}}.keys()

print('A' in v)
print('C' in v)
# True

print('a' in v)
print(('A', 'C') in v)
# False

print(['A', 'C'] in v)
# TypeError: unhashable type: 'list'
Enter fullscreen mode Exit fullscreen mode
v = {'A':'B', 'C':{'D':'E'}}
v = {'A':'B', 'C':{'D':'E'}}.keys()

print(v)
# # {'A': 'B', 'C': {'D': 'E'}}

print('A' not in v)
print('C' not in v)
# False

print('a' not in v)
print(('A', 'C') not in v)
# True

print(['A', 'C'] not in v)
# TypeError: unhashable type: 'list'
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

v = {'A':'B', 'C':{'D':'E'}}.values()

print('B' in v)
print({'D':'E'} in v)
# True

print('b' in v)
print(['B', {'D':'E'}] in v)
# False
Enter fullscreen mode Exit fullscreen mode
v = {'A':'B', 'C':{'D':'E'}}.values()

print('B' not in v)
print({'D':'E'} not in v)
# False

print('b' not in v)
print(['B', {'D':'E'}] not in v)
# True
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

v = {'A':'B', 'C':{'D':'E'}}.items()

print(('A', 'B') in v)
print(('C', {'D':'E'}) in v)
# True

print(('a', 'b') in v)
print('A' in v)
print('B' in v)
print('C' in v)
print({'D':'E'} in v)
print([('A', 'B'), ('C', {'D':'E'})] in v)
# False
Enter fullscreen mode Exit fullscreen mode
v = {'A':'B', 'C':{'D':'E'}}.items()

print(('A', 'B') not in v)
print(('C', {'D':'E'}) not in v)
# False

print(('a', 'b') not in v)
print('A' not in v)
print('B' not in v)
print('C' not in v)
print({'D':'E'} not in v)
print([('A', 'B'), ('C', {'D':'E'})] not in v)
# True
Enter fullscreen mode Exit fullscreen mode

A dictionary can be checked if the dictionary is and isn't referred to by two variables with is keyword and with is and not keyword respectively as shown below:

*Memo:

  • Dictionary(Dict) literals with is keyword and with is and not keyword don't get warnings respectively.

<dict>:

v1 = {0:1, 2:3}
v2 = {0:1, 2:3}
v3 = v1

print(v1 is v2) # False
print(v1 is v3) # True

print(v1 is not v2) # True
print(v1 is not v3) # False
Enter fullscreen mode Exit fullscreen mode
print({0:1, 2:3} is {0:1, 2:3}) # False
print({0:1, 2:3} is {0:1})      # False

print({0:1, 2:3} is not {0:1, 2:3}) # True
print({0:1, 2:3} is not {0:1})      # True
Enter fullscreen mode Exit fullscreen mode

<dict.keys()>:

v1 = {0:1, 2:3}.keys()
v2 = {0:1, 2:3}.keys()
v3 = v1

print(v1 is v2) # False
print(v1 is v3) # True

print(v1 is not v2) # True
print(v1 is not v3) # False
Enter fullscreen mode Exit fullscreen mode
print({0:1, 2:3}.keys() is {0:1, 2:3}.keys()) # False
print({0:1, 2:3}.keys() is {0:1}.keys())      # False

print({0:1, 2:3}.keys() is not {0:1, 2:3}.keys()) # True
print({0:1, 2:3}.keys() is not {0:1}.keys())      # True
Enter fullscreen mode Exit fullscreen mode

<dict.values()>:

v1 = {0:1, 2:3}.values()
v2 = {0:1, 2:3}.values()
v3 = v1

print(v1 is v2) # False
print(v1 is v3) # True

print(v1 is not v2) # True
print(v1 is not v3) # False
Enter fullscreen mode Exit fullscreen mode
print({0:1, 2:3}.values() is {0:1, 2:3}.values()) # False
print({0:1, 2:3}.values() is {0:1}.values())      # False

print({0:1, 2:3}.values() is not {0:1, 2:3}.values()) # True
print({0:1, 2:3}.values() is not {0:1}.values())      # True
Enter fullscreen mode Exit fullscreen mode

<dict.items()>:

v1 = {0:1, 2:3}.items()
v2 = {0:1, 2:3}.items()
v3 = v1

print(v1 is v2) # False
print(v1 is v3) # True

print(v1 is not v2) # True
print(v1 is not v3) # False
Enter fullscreen mode Exit fullscreen mode
print({0:1, 2:3}.items() is {0:1, 2:3}.items()) # False
print({0:1, 2:3}.items() is {0:1}.items())      # False

print({0:1, 2:3}.items() is not {0:1, 2:3}.items()) # True
print({0:1, 2:3}.items() is not {0:1}.items())      # True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)