Can You Use a List as a Key in a Python Dictionary?

Dive into the world of dictionary keys and explore why using lists as keys is not possible in Python. Learn about the importance of immutability and discover alternative ways to achieve your desired d …

Updated August 26, 2023



Dive into the world of dictionary keys and explore why using lists as keys is not possible in Python. Learn about the importance of immutability and discover alternative ways to achieve your desired data structure.

Let’s imagine you have a collection of student records, each containing their name (a string), ID number (an integer), and grades (a list). You want to organize these records efficiently so you can easily look up information about a specific student using their ID or even their name. This is where dictionaries shine!

Dictionaries in Python are like labeled containers. They store pairs of keys and values. Think of the keys as labels, and the values as the information associated with those labels. For example:

student_records = {
    "12345": {"name": "Alice", "grades": [90, 85, 92]},
    "67890": {"name": "Bob", "grades": [88, 95, 82]}
}

Here, student IDs (“12345”, “67890”) are used as keys to access the corresponding student information (a dictionary containing their name and grades).

But wait, can we use a list like [88, 95, 82] as a key in our dictionary? The answer is no.

Why Lists Can’t Be Dictionary Keys:

Python dictionaries require keys to be immutable. This means the key itself cannot be changed after it’s created. Think of it like writing a label on a box – once you write it, you can’t erase or rewrite the label without creating a new box altogether.

Lists, however, are mutable. You can add items, remove items, or change the order of elements within a list. This mutability makes them unsuitable as dictionary keys because the dictionary needs to rely on a consistent and unchanging key to find the associated value.

Imagine this scenario:

You use Bob’s grades [88, 95, 82] as the key in your student_records dictionary. But then, Bob takes a new test, and his grade list becomes [88, 95, 82, 90]. Now, the key has changed! The dictionary won’t be able to find Bob’s information because it’s looking for the old key [88, 95, 82], which no longer exists.

What are the Alternatives?

If you want to use a list-like structure as a way to identify data in your dictionary, here are some approaches:

  1. Convert the List to a Tuple: Tuples are similar to lists but are immutable. You can convert a list into a tuple using the tuple() function:
grades_tuple = tuple(Bobs_grades)  # Convert the list to a tuple
student_records[grades_tuple] = {"name": "Bob", ...} # Use the tuple as the key
  1. Use a Hash Function:

Hash functions take any input (like a list) and convert it into a unique, fixed-size output called a hash. This hash can be used as an immutable key in your dictionary. Python libraries like hashlib provide built-in hash function implementations.

Let me know if you’d like to explore these alternatives in more detail!


Stay up to date on the latest in Computer Vision and AI

Intuit Mailchimp