*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:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotrespectively.
<dict>:
# Non-empty dict
print(bool({0:0}))
print(bool({():{}}))
# True
# Empty dict
print(bool({}))
# False
# Non-empty dict
print(not {0:0})
print(not {():{}})
# False
# Empty dict
print(not {})
# True
<dict.keys()>:
# Non-empty dict
print(bool({0:0}.keys()))
print(bool({():{}}.keys()))
# True
# Empty dict
print(bool({}.keys()))
# False
# Non-empty dict
print(not {0:0}.keys())
print(not {():{}}.keys())
# False
# Empty dict
print(not {}.keys())
# True
<dict.values()>:
# Non-empty dict
print(bool({0:0}.values()))
print(bool({():{}}.values()))
# True
# Empty dict
print(bool({}.values()))
# False
# Non-empty dict
print(not {0:0}.values())
print(not {():{}}.values())
# False
# Empty dict
print(not {}.values())
# True
<dict.items()>:
# Non-empty dict
print(bool({0:0}.items()))
print(bool({():{}}.items()))
# True
# Empty dict
print(bool({}.items()))
# False
# Non-empty dict
print(not {0:0}.items())
print(not {():{}}.items())
# False
# Empty dict
print(not {}.items())
# True
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'
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'
<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
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
<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
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
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
isandis notdon'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
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
<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
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
<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
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
<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
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
Top comments (0)