*Memo:
- My post explains a dictionary (1).
- My post explains a dictionary (2).
- My post explains a dictionary (3).
- My post explains a dictionary (4).
- My post explains a dictionary (5).
- My post explains a dictionary (7).
- My post explains a dictionary (8).
- My post explains a dictionary (9).
A dictionary cannot be enlarged with * and a number as shown below:
<dict>:
v = {'name':'John', 'age':36} * 3
# TypeError: unsupported operand type(s) for *: 'dict' and 'int'
<dict.keys()>:
v = {'name':'John', 'age':36}.keys() * 3
# TypeError: unsupported operand type(s) for *: 'dict_keys' and 'int'
<dict.values()>:
v = {'name':'John', 'age':36}.values() * 3
# TypeError: unsupported operand type(s) for *: 'dict_values' and 'int'
<dict.items()>:
v = {'name':'John', 'age':36}.items() * 3
# TypeError: unsupported operand type(s) for *: 'dict_items' and 'int'
A dictionary and other dictionaries cannot be concatenated with + as shown below:
<dict>:
v = {0:1, 2:3, 4:5} + {6:7, 8:9} + {10:11, 12:13, 14:15, 16:17}
# TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
<dict.keys()>:
v = {0:1, 2:3, 4:5}.keys() + {6:7, 8:9}.keys() \
+ {10:11, 12:13, 14:15, 16:17}.keys()
# TypeError: unsupported operand type(s) for +: 'dict_keys' and 'dict_keys'
<dict.values()>:
v = {0:1, 2:3, 4:5}.values() + {6:7, 8:9}.values() \
+ {10:11, 12:13, 14:15, 16:17}.values()
# TypeError: unsupported operand type(s) for +: 'dict_values' and 'dict_values'
<dict.items()>:
v = {0:1, 2:3, 4:5}.items() + {6:7, 8:9}.items() \
+ {10:11, 12:13, 14:15, 16:17}.items()
# TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'
A dictionary and other dictionary can return:
- only all the keys or both all the keys and values in them with
'|'(Union: A ∪ B). - only their common keys or both their common keys and values with
'&'(Intersection: A ∩ B). - only the keys or both the keys and values in the dictionary which aren't in other dictionary with
'-'(Difference: A - B). - only the keys or both the keys and values in either the dictionary or other dictionary but not both with
'^'(Symmetric Difference: A Δ B).
*Memo:
-
dictworks only for'|'. -
dict.keys() and dict.items() work for all
'|','&','-'and'^'. -
dict.values() gets error for all
'|','&','-'and'^'so only values cannot be checked while only keys or both keys and values can be checked.
<dict>:
v1 = {0:1, 8:9}
v2 = {0:1, 4:5, 8:9}
v3 = {0:1, 2:3, 6:7, 8:9}
print(v1 | v2)
# {0: 1, 8: 9, 4: 5}
print(v1 | v3)
# {0: 1, 8: 9, 2: 3, 6: 7}
print(v2 | v3)
# {0: 1, 4: 5, 8: 9, 2: 3, 6: 7}
print(v1 | v2 | v3)
# {0: 1, 8: 9, 4: 5, 2: 3, 6: 7}
v = {0:1, 2:3, 4:5, 6:7} & {0:1, 4:5, 8:9}
# TypeError: unsupported operand type(s) for &: 'dict' and 'dict'
v = {0:1, 2:3, 4:5, 6:7} - {0:1, 4:5, 8:9}
# TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
v = {0:1, 2:3, 4:5, 6:7} ^ {0:1, 4:5, 8:9}
# TypeError: unsupported operand type(s) for ^: 'dict' and 'dict'
<dict.keys()>:
v1 = {0:1, 8:9}.keys()
v2 = {0:1, 4:5, 8:9}.keys()
v3 = {0:1, 2:3, 6:7, 8:9}.keys()
print(v1 | v2)
# {0, 8, 4}
print(v1 | v3)
# {0, 8, 2, 6}
print(v2 | v3)
# {0, 2, 4, 6, 8}
print(v1 | v2 | v3)
# {0, 2, 4, 6, 8}
v1 = {0:1, 2:3, 4:5, 6:7}.keys()
v2 = {0:1, 4:5, 8:9}.keys()
v3 = {4:5, 6:7}.keys()
print(v1 & v2)
# {0, 4}
print(v1 & v3)
# {4, 6}
print(v2 & v3)
# {4}
print(v1 & v2 & v3)
# {4}
v1 = {0:1, 2:3, 4:5, 6:7}.keys()
v2 = {0:1, 4:5, 8:9}.keys()
v3 = {4:5, 6:7}.keys()
print(v1 - v2)
# {2, 6}
print(v1 - v3)
# {0, 2}
print(v2 - v3)
# {0, 8}
print(v1 - v2 - v3)
# {2}
v1 = {0:1, 2:3, 4:5, 6:7}.keys()
v2 = {0:1, 4:5, 8:9}.keys()
v3 = {4:5, 6:7}.keys()
print(v1 ^ v2)
# {8, 2, 6}
print(v1 ^ v3)
# {0, 2}
print(v2 ^ v3)
# {0, 8, 6}
print(v1 ^ v2 ^ v3)
# {8, 2, 4}
<dict.values()>:
v = {0:1, 8:9}.values() | {0:1, 4:5, 8:9}.values()
# TypeError: unsupported operand type(s) for |: 'dict_values' and 'dict_values'
v = {0:1, 2:3, 4:5, 6:7}.values() & {0:1, 4:5, 8:9}.values()
# TypeError: unsupported operand type(s) for &: 'dict_values' and 'dict_values'
v = {0:1, 2:3, 4:5, 6:7}.values() - {0:1, 4:5, 8:9}.values()
# TypeError: unsupported operand type(s) for -: 'dict_values' and 'dict_values'
v = {0:1, 2:3, 4:5, 6:7}.values() ^ {0:1, 4:5, 8:9}.values()
# TypeError: unsupported operand type(s) for ^: 'dict_values' and 'dict_values'
<dict.items()>:
v1 = {0:1, 8:9}.items()
v2 = {0:1, 4:5, 8:9}.items()
v3 = {0:1, 2:3, 6:7, 8:9}.items()
print(v1 | v2)
# {(0, 1), (4, 5), (8, 9)}
print(v1 | v3)
# {(0, 1), (6, 7), (8, 9), (2, 3)}
print(v2 | v3)
# {(0, 1), (2, 3), (6, 7), (4, 5), (8, 9)}
print(v1 | v2 | v3)
# {(0, 1), (2, 3), (6, 7), (4, 5), (8, 9)}
v1 = {0:1, 2:3, 4:5, 6:7}.items()
v2 = {0:1, 4:5, 8:9}.items()
v3 = {4:5, 6:7}.items()
print(v1 & v2)
# {(0, 1), (4, 5)}
print(v1 & v3)
# {(6, 7), (4, 5)}
print(v2 & v3)
# {(4, 5)}
print(v1 & v2 & v3)
# {(4, 5)}
v1 = {0:1, 2:3, 4:5, 6:7}.items()
v2 = {0:1, 4:5, 8:9}.items()
v3 = {4:5, 6:7}.items()
print(v1 - v2)
# {(2, 3), (6, 7)}
print(v1 - v3)
# {(0, 1), (2, 3)}
print(v2 - v3)
# {(0, 1), (8, 9)}
print(v1 - v2 - v3)
# {(2, 3)}
v1 = {0:1, 2:3, 4:5, 6:7}.items()
v2 = {0:1, 4:5, 8:9}.items()
v3 = {4:5, 6:7}.items()
print(v1 ^ v2)
# {(2, 3), (6, 7), (8, 9)}
print(v1 ^ v3)
# {(0, 1), (2, 3)}
print(v2 ^ v3)
# {(0, 1), (6, 7), (8, 9)}
print(v1 ^ v2 ^ v3)
# {(2, 3), (4, 5), (8, 9)}
Top comments (0)