*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 (6).
- My post explains a dictionary (7).
- My post explains a dictionary (8).
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 dictionaries can return only all the keys or both all the keys and values in them with '|' (Union: A ∪ B) as shown below:
*Memo:
- Only all the values cannot be checked.
-
dict, dict.keys() and dict.items() work. - dict.values() gets error.
<dict>:
v = {0:1, 2:3, 4:5} | {6:7, 8:9} | {10:11, 12:13, 14:15, 16:17}
print(v)
# {0: 1, 2: 3, 4: 5, 6: 7, 8: 9, 10: 11, 12: 13, 14: 15, 16: 17}
<dict.keys()>:
v = {0:1, 2:3, 4:5}.keys() | {6:7, 8:9}.keys() \
| {10:11, 12:13, 14:15, 16:17}.keys()
print(v)
# {0, 2, 4, 6, 8, 10, 12, 14, 16}
<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()
print(v)
# {(0, 1), (10, 11), (12, 13), (6, 7), (8, 9),
# (2, 3), (4, 5), (14, 15), (16, 17)}
A dictionary and other dictionaries cannot return their common keys and/or values with '&' (Intersection: A ∩ B) as shown below:
*Memo:
-
dict.keys()anddict.items()work but an empty set is returned. -
dictanddict.values()get error.
<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()
print(v)
# set()
<dict.values()>:
v = {0:1, 2:3, 4:5}.values() & {6:7, 8:9}.values() \
& {10:11, 12:13, 14:15, 16:17}.values()
print(v)
# 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()
print(v)
# set()
A dictionary and other dictionaries can return only the keys or both the keys and values in the dictionary which aren't in other dictionaries with '-' (Difference: A - B) as shown below:
*Memo:
- Only the values cannot be returned.
-
dict.keys()anddict.items()work. -
dictanddict.values()get error.
<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()
print(v)
# {0, 2, 4}
<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()
print(v)
# {(0, 1), (4, 5), (2, 3)}
A dictionary and other dictionary can return only the keys or both the keys and values in the dictionary but not in other dictionary or not in the dictionary but in other dictionary with '^' (Symmetric Difference: A Δ B) as shown below:
*Memo:
- Only the values cannot be returned.
-
dict.keys()anddict.items()work. -
dictanddict.values()get error.
<dict>:
print({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()>:
print({0:1, 2:3, 4:5, 6:7}.keys() ^ {0:1, 4:5, 8:9}.keys())
# {8, 2, 6}
<dict.values()>:
print({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()>:
print({0:1, 2:3, 4:5, 6:7}.items() ^ {0:1, 4:5, 8:9}.items())
# {(2, 3), (6, 7), (8, 9)}
Top comments (0)