In Python, we use dictionaries to check if an item is present or not . Dictionaries use key:value pair to search if a key is present or not and if the key is present what is its value . We can use integer, string, tuples as dictionary keys but cannot use list as a key of it . The reason is explained below .
How does Python Dictionaries search their keys
If the Python dictionaries just iterate over their keys to check if the given keys are present or not it would take O(N) time. But python dictionaries take O(1) to check if the key is present or not. So, how dictionaries search the key, for every key it generates a hash value for the key and by this hash value, it can track its elements.
Problems if lists were used as a key of dictionaries
Lists are mutable objects which means that we can change values inside a list append or delete values of the list . So if a hash function is generated from a list and then the items of the lists changed the dictionary will generate a new hash value for this list and could not find it.
For example, If the list is
a = [1, 2, 3, 4, 5] and supposes hash of a list is the sum of values inside the list. So hash(a) = 15. Now we append 6 to a . So
a = [1, 2, 3, 4, 5, 6] hash(a) = 21. So hash value of a changed. Therefore it can not be found inside the dictionary.
Another problem is different lists with the same hash value. If
b = [5, 5, 5] hash(b) = 15. So if a (From the above example with the same hash value) is present is inside the dictionary, and if we search for b. Then the dictionary may give us the wrong result.
How to deal with this
We can change the list into immutable objects like string or tuple and then can use it as a key. Below is the implementation of the approach.
[1, 2, 3, 4, 5] : 1 (1, 2, 3, 4, 5) : 1
- Python | Filter dictionary key based on the values in selective list
- Use get() method to create a dictionary in Python from a list of elements
- Python | Ways to remove a key from dictionary
- Python | Check whether given key already exists in a dictionary
- Python | Accessing Key-value in Dictionary
- Python | Get key from value in Dictionary
- Add a key:value pair to dictionary in Python
- Python | Remove item from dictionary when key is unknown
- Python | Get key with maximum value in Dictionary
- Python | Convert byteString key:value pair of dictionary to String
- Python | Sum values for each key in nested dictionary
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Sum list of dictionaries with same key
- Print anagrams together in Python using List and Dictionary
- Python | Convert a list of Tuples into Dictionary
- Counting the frequencies in a list using dictionary in Python
- Python | Count number of items in a dictionary value that is a list
- Python | Sort the list alphabetically in a dictionary
- Python | Get dictionary keys as a list
- Python | Initialize a dictionary with only keys from a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.