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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Convert list to Single Dictionary Key Value list
- Python - Assigning Key values to list elements from Value list Dictionary
- Python - Convert String List to Key-Value List dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python | Filter dictionary key based on the values in selective list
- Python - Dictionary Key's Product in list
- Python - Check for Key in Dictionary Value list
- Python - Convert key-values list to flat dictionary
- Python - Key Value list pairings in Dictionary
- Python - Remove Key from Dictionary List
- Python - Unnest single Key Nested Dictionary List
- Python - Change type of key in Dictionary list
- Python - Sort Dictionary key and values List
- Group List of Dictionary Data by Particular Key in Python
- Python program to find Maximum value from dictionary whose key is present in the list
- Python - Sort Dictionary List by Key's ith Index value
- Use get() method to create a dictionary in Python from a list of elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.