*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).
A dictionary and other dictionary can be checked if all the keys and/or values in them are and aren't the same with == and != respectively as shown below:
<dict>:
v = {0:1, 2:3, 4:5}
print(v == {0:1, 2:3, 4:5}) # True
print(v == {0:1, 2:3}) # False
print(v == {0:1, 4:5}) # 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 == {}) # False
v = {0:1, 2:3, 4:5}
print(v != {0:1, 2:3, 4:5}) # False
print(v != {0:1, 2:3}) # True
print(v != {0:1, 4:5}) # 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 != {}) # True
<dict.keys()>:
v = {0:1, 2:3, 4:5}.keys()
print(v == {0:1, 2:3, 4:5}.keys()) # True
print(v == {0:1, 2:3}.keys()) # False
print(v == {0:1, 4:5}.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 == {}.keys()) # False
v = {0:1, 2:3, 4:5}.keys()
print(v != {0:1, 2:3, 4:5}.keys()) # False
print(v != {0:1, 2:3}.keys()) # True
print(v != {0:1, 4:5}.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 != {}.keys()) # True
<dict.values()>:
v = {0:1, 2:3, 4:5}.values()
print(v == {0:1, 2:3, 4:5}.values()) # False
print(v == {0:1, 2:3}.values()) # False
print(v == {0:1, 4:5}.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 == {}.values()) # False
v = {0:1, 2:3, 4:5}.values()
print(v != {0:1, 2:3, 4:5}.values()) # True
print(v != {0:1, 2:3}.values()) # True
print(v != {0:1, 4:5}.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 != {}.values()) # True
<dict.items()>:
v = {0:1, 2:3, 4:5}.items()
print(v == {0:1, 2:3, 4:5}.items()) # True
print(v == {0:1, 2:3}.items()) # False
print(v == {0:1, 4:5}.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 == {}.items()) # False
v = {0:1, 2:3, 4:5}.items()
print(v != {0:1, 2:3, 4:5}.items()) # False
print(v != {0:1, 2:3}.items()) # True
print(v != {0:1, 4:5}.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 != {}.items()) # True
A dictionary and other dictionary can be checked if only all the keys or both all the keys and values in:
- the dictionary are in other dictionary with
<=. - other dictionary are in the dictionary with
>=. - the dictionary and other elements are in other dictionary with
<. - other dictionary and other elements are in the dictionary with
>.
*Memo:
- Only all the values cannot be checked.
- dict.keys() and dict.items() work.
-
dictand dict.values() get error.
<dict>:
v = {0:1, 2:3, 4:5}
print(v <= {0:1, 2:3, 4:5})
# TypeError: '<=' not supported between instances of 'dict' and 'dict'
print(v >= {0:1, 2:3, 4:5})
# TypeError: '>=' not supported between instances of 'dict' and 'dict'
print(v < {0:1, 2:3, 4:5})
# TypeError: '<' not supported between instances of 'dict' and 'dict'
print(v > {0:1, 2:3, 4:5})
# TypeError: '>' not supported between instances of 'dict' and 'dict'
<dict.keys()>:
v = {0:1, 2:3, 4:5}.keys()
print(v <= {0:1, 2:3, 4:5}.keys()) # True
print(v >= {0:1, 2:3, 4:5}.keys()) # True
print(v < {0:1, 2:3, 4:5}.keys()) # False
print(v > {0:1, 2:3, 4:5}.keys()) # False
<dict.values()>:
v = {0:1, 2:3, 4:5}.values()
print(v <= {0:1, 2:3, 4:5}.values())
# TypeError: '<=' not supported between instances of 'dict_values' and
# 'dict_values'
print(v >= {0:1, 2:3, 4:5}.values())
# TypeError: '>=' not supported between instances of 'dict_values' and
# 'dict_values'
print(v < {0:1, 2:3, 4:5}.values())
# TypeError: '<' not supported between instances of 'dict_values' and
# 'dict_values'
print(v > {0:1, 2:3, 4:5}.values())
# TypeError: '>' not supported between instances of 'dict_values' and
# 'dict_values'
<dict.items()>:
v = {0:1, 2:3, 4:5}.items()
print(v <= {0:1, 2:3, 4:5}.items()) # True
print(v >= {0:1, 2:3, 4:5}.items()) # True
print(v < {0:1, 2:3, 4:5}.items()) # False
print(v > {0:1, 2:3, 4:5}.items()) # False
A dictionary and other dictionary can be checked if they have and don't have only their common keys or both their common keys and values with bool() and & and with not keyword and & respectively as shown below:
*Memo:
- Only their common values cannot be checked.
-
dict.keys()anddict.items()work. -
dictanddict.values()get error.
<dict>:
v = {0:1, 2:3, 4:5}
print(bool(v & {2:3, 6:7}))
print(not (v & {6:7, 8:9}))
# TypeError: unsupported operand type(s) for &: 'dict' and 'dict'
<dict.keys()>:
v = {0:1, 2:3, 4:5}.keys()
print(bool(v & {2:3, 6:7}.keys())) # True
print(not (v & {2:3, 6:7}.keys())) # False
<dict.values()>:
v = {0:1, 2:3, 4:5}.values()
print(bool(v & {2:3, 6:7}.values()))
print(not (v & {2:3, 6:7}.values()))
# TypeError: unsupported operand type(s) for &: 'dict_values' and 'dict_values'
<dict.items()>:
v = {0:1, 2:3, 4:5}.items()
print(bool(v & {2:3, 6:7}.items())) # True
print(not (v & {2:3, 6:7}.items())) # False
Top comments (0)