DEV Community

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

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

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 respectively.

<dict>:

# Non-empty dict
print(bool({0:0}))
print(bool({():{}}))
# True

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

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

<dict.keys()>:

# Non-empty dict
print(bool({0:0}.keys()))
print(bool({():{}}.keys()))
# True

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

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

<dict.values()>:

# Non-empty dict
print(bool({0:0}.values()))
print(bool({():{}}.values()))
# True

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

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

<dict.items()>:

# Non-empty dict
print(bool({0:0}.items()))
print(bool({():{}}.items()))
# True

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

# Empty dict
print(not {}.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 and not in respectively as shown below:

<dict & dict.keys()>:

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

print(v)
# dict_keys(['A', 'C'])

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

print(('A',) in v)
print(('C',) in v)
print(('A', 'C') in v)
print(() 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)
# dict_keys(['A', 'C'])

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

print(('A',) not in v)
print(('C',) not in v)
print(('A', 'C') not in v)
print(() 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(v)
# dict_values(['B', {'D': 'E'}])

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(v)
# dict_values(['B', {'D': 'E'}])

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(v)
# dict_items([('A', 'B'), ('C', {'D': 'E'})])

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

print('A' in v)
print('B' in v)
print('C' 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' not in v)
print('B' not in v)
print('C' 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 and is not respectively as shown below:

*Memo:

  • Dictionary(Dict) literals with is and is not 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)