*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:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotkeyword respectively.
<dict>:
print(bool({0:0})) # dict
print(bool({():{}})) # dict(Empty tuple:Empty dict)
# True
print(bool({})) # Empty dict
# False
print(not {0:0}) # dict
print(not {():{}}) # dict(Empty tuple:Empty dict)
# False
print(not {}) # Empty dict
# True
<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
print(not {0:0}.keys()) # dict.keys()
print(not {():{}}.keys()) # dict(Empty tuple:Empty dict).keys()
# False
print(not {}.keys()) # Empty dict.keys()
# True
<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
print(not {0:0}.values()) # dict.values()
print(not {():{}}.values()) # dict(Empty tuple:Empty dict).values()
# False
print(not {}.values()) # Empty dict.values()
# True
<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
print(not {0:0}.items()) # dict.items()
print(not {():{}}.items()) # dict(Empty tuple:Empty dict).items()
# False
print(not {}.items()) # Empty dict.items()
# True
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'
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'
<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
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
<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
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
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
iskeyword and withisandnotkeyword 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
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)