How to Understand the Key Differences Between List and Dictionary in Python?

As a Python developer working on a project for a US-based company, I recently encountered a situation where I needed to decide whether to use a list or a dictionary to store and manipulate data efficiently. In this article, I will explain how to understand the key differences between list and dictionary in Python. I will share my findings and help you understand when to use each data structure with the help of practical examples.

List in Python

A list in Python is an ordered collection of items, which can be of different data types such as integers, floats, strings, or even other lists. Lists are used to store data in an ordered and sequential pattern. Lists are mutable, meaning you can change, add, or remove elements after the list has been created.

Example:

employees = ["John", "Emma", "Michael", "Olivia", "William"]

Read How to Split a Python List Into Evenly Sized Chunks?

Dictionary in Python

A dictionary in Python is an unordered collection of key-value pairs. Each key in a dictionary is unique and is used to access its corresponding value. Dictionaries are used to store large amounts of data for easy and quick access. Like lists, dictionaries are mutable.

Example:

employee_details = {
    "John": {"age": 35, "department": "Sales"},
    "Emma": {"age": 28, "department": "Marketing"},
    "Michael": {"age": 42, "department": "Engineering"}
}

Check out How to Add Tuples to Lists in Python?

Key Differences Between List and Dictionary

  1. Order: Lists are linear whereas dictionaries store the data in key-value pairs. Lists maintain the order of elements, while dictionaries do not have a specific order.
  2. Accessing Elements: In lists, elements are accessed by their index, which starts from 0. In dictionaries, elements are accessed by their unique keys.
  3. Mutability: Both lists and dictionaries are mutable, but the way they are modified differs. In lists, you can change, add, or remove elements by index. In dictionaries, you can change, add, or remove key-value pairs.
  4. Duplicate Elements: Lists allow duplicate elements, while dictionary keys must be unique. However, dictionary values can be duplicated.
  5. Performance: A dict is a hash table, so it is really fast to find the keys. So between dict and list, dict would be faster. Dictionaries are optimized for fast lookups by keys, while lists are better for scenarios where order matters.

Read How to Convert Dictionary to List of Tuples in Python?

When to Use a List

  • When you need to maintain the order of elements
  • When you need to store duplicate elements
  • When you need to access elements by their index

When to Use a Dictionary

  • When you need to store key-value pairs
  • When you need fast lookups by keys
  • When you need to store large amounts of data and access them efficiently

Check out How to Create a Tuple from the List in Python?

Real-World Example

Let’s consider a real-world scenario where you need to store information about employees in a US-based company.

Using a list:

employees = [
    ["John", 35, "Sales"],
    ["Emma", 28, "Marketing"],
    ["Michael", 42, "Engineering"],
    ["Olivia", 31, "Marketing"],
    ["William", 29, "Sales"]
]
print(employees)

Output:

[['John', 35, 'Sales'], ['Emma', 28, 'Marketing'], ['Michael', 42, 'Engineering'], ['Olivia', 31, 'Marketing'], ['William', 29, 'Sales']]

I executed the above example code and added the screenshot below.

Understand the Key Differences Between List and Dictionary in Python

Using a dictionary:

employee_details = {
    "John": {"age": 35, "department": "Sales"},
    "Emma": {"age": 28, "department": "Marketing"},
    "Michael": {"age": 42, "department": "Engineering"},
    "Olivia": {"age": 31, "department": "Marketing"},
    "William": {"age": 29, "department": "Sales"}
}
print(employee_details)

Output:

{'John': {'age': 35, 'department': 'Sales'}, 'Emma': {'age': 28, 'department': 'Marketing'}, 'Michael': {'age': 42, 'department': 'Engineering'}, 'Olivia': {'age': 31, 'department': 'Marketing'}, 'William': {'age': 29, 'department': 'Sales'}}

I executed the above example code and added the screenshot below.

How to Understand the Key Differences Between List and Dictionary in Python

In this case, using a dictionary would be more efficient because:

  • You can quickly access an employee’s details using their name as the key
  • You can easily update or remove an employee’s information
  • The dictionary provides a more organized and readable structure

Check out How to Create a Dictionary from Two Lists in Python?

Summary

AspectListDictionary
OrderMaintains orderUnordered
Accessing ElementsBy indexBy unique keys
MutabilityMutableMutable
Duplicate ElementsAllows duplicatesKeys must be unique
PerformanceSlower lookupsFaster lookups by keys

Conclusion

In this tutorial, I have explained how to understand the key differences between list and dictionary in Python. I discussed what are list and dictionary in Python, the key differences between list and dictionary, When to use list, and when to use dictionary. I also covered a real-world example and summary.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.