What is a dictionary in Python? How does it differ from a list?

This article explains the fundamental difference between dictionaries and lists in Python, two essential data structures. We’ll explore their characteristics, use cases, and why understanding them is …

Updated August 26, 2023



This article explains the fundamental difference between dictionaries and lists in Python, two essential data structures. We’ll explore their characteristics, use cases, and why understanding them is crucial for aspiring Python programmers.

Dictionaries and lists are both fundamental data structures in Python, used to store collections of data. However, they differ significantly in how they organize and access that data. Understanding these differences is key to writing efficient and effective Python code.

What is a Dictionary?

Imagine a real-world dictionary where you look up words using their definitions. A Python dictionary works similarly: it stores data as key-value pairs. Each key is unique (like a word in a dictionary), and it’s associated with a specific value (like the word’s definition).

Here’s a simple example:

student = {
    "name": "Alice", 
    "age": 25,
    "major": "Computer Science"
}

In this dictionary, "name", "age", and "major" are the keys, while "Alice", 25, and "Computer Science" are their corresponding values.

Key Points About Dictionaries:

  • Unordered: Unlike lists, dictionaries don’t store items in a specific order. You access data using keys, not by position.
  • Mutable: You can add new key-value pairs, modify existing values, or remove entries from a dictionary after it’s created.
  • Keys Must Be Unique: Each key within a dictionary must be distinct. Duplicate keys are not allowed.

Accessing Values in a Dictionary:

To retrieve a value from a dictionary, you use its corresponding key:

print(student["name"])  # Output: Alice
print(student["age"])   # Output: 25 

What is a List?

A list is a sequential collection of items. Think of it like an ordered list:

fruits = ["apple", "banana", "cherry"]

In this list, "apple" is at index 0, "banana" at index 1, and "cherry" at index 2.

Key Points About Lists:

  • Ordered: Items in a list maintain their order.
  • Mutable: You can add, remove, or modify elements within a list.
  • Allows Duplicates: A list can contain the same element multiple times.

Accessing Values in a List:

You access values in a list using their numerical index:

print(fruits[0])  # Output: apple 
print(fruits[2])  # Output: cherry

Why is Understanding Dictionaries and Lists Important for Learning Python?

Choosing the right data structure is essential for writing clean, efficient code. Knowing when to use a dictionary versus a list will make you a better programmer.

  • Use Dictionaries When: You need to store related data with unique identifiers (keys). Examples include storing user profiles (name as key, details as value), product catalogs (product ID as key, product information as value), or configuration settings.

  • Use Lists When: You need an ordered collection of items where the order matters, and you might have duplicates. Examples include storing a list of tasks, tracking scores in a game, or representing a sequence of events.

Let me know if you’d like to explore specific examples or have any more questions!


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

Intuit Mailchimp