Junior — Middle
Have you used the Allure Enterprise tool in your projects?
sobes.tech AI
Answer from AI
In Python, a key in a dictionary must satisfy two conditions:
- Hashability — the key must be immutable and have a
__hash__()method. For example, strings, numbers, tuples of immutable elements. - Comparability — keys are compared by value to find the corresponding element.
Example:
my_dict = {"apple": 1, (1, 2): "tuple key", 42: "number key"}
for key in my_dict.keys():
print(f"Key: {key}, type: {type(key)}")
Trying to use a mutable type (like a list) as a key will result in a TypeError: unhashable type: 'list'.
Thus, a key is a hashable and immutable object used for quick access to values in a dictionary.