What's the primary benefit of using tuples as keys in dictionaries over lists?
- Tuples are faster to access.
- Tuples are immutable.
- Tuples are more memory-efficient.
- Tuples guarantee order.
The primary benefit of using tuples as keys in dictionaries is that tuples are immutable, while lists are not. This immutability ensures that the keys won't change, making tuples suitable for use as dictionary keys. Lists can't be used as keys because they can change.
Loading...
Related Quiz
- Unlike lists, tuples are _______ in Python, meaning they cannot be modified after they are created.
- How do you access the first item in a tuple named my_tuple?
- The _____ method in a metaclass is executed when a new class is created.
- In which scenario would you typically use the @staticmethod decorator in a class?
- How can you remove an element from a set without raising an error if the element doesn't exist?