Table of contents
1.
Introduction
2.
Creating a List of Dictionaries
2.1.
Syntax
2.2.
Example
2.3.
Python
3.
Accessing Data in a List of Dictionaries
3.1.
Accessing Individual Dictionaries
3.2.
Python
3.3.
Accessing Values Using Keys
3.4.
Python
4.
Adding and Removing Elements
4.1.
Adding a Dictionary to the List
4.2.
Python
4.3.
Removing a Dictionary from the List
4.4.
Python
5.
Modifying Dictionaries in a List
5.1.
Updating Values
5.2.
Python
5.3.
Adding New Key-Value Pairs
5.4.
Python
5.5.
Removing Key-Value Pairs
5.6.
Python
6.
Sorting a List of Dictionaries
6.1.
Sorting by a Key
6.2.
Python
7.
Frequently Asked Questions
7.1.
How do I update a specific dictionary in a list?
7.2.
How can I remove a dictionary from a list based on a condition?
7.3.
Can I sort a list of dictionaries by multiple keys?
8.
Conclusion
Last Updated: Jul 25, 2024
Easy

List of Dictionaries in Python

Introduction

Python lists and dictionaries are versatile data structures that allow for efficient storage and manipulation of data. A list of dictionaries is a common way to represent a collection of records or complex data structures in Python. 

List of Dictionaries in Python

This article will guide you through the creation, manipulation, and usage of lists of dictionaries in Python, providing syntax, examples, and best practices.

Creating a List of Dictionaries

Syntax

To create a list of dictionaries, you can use the following syntax:

list_of_dicts = [
    {"key1": "value1", "key2": "value2"},
    {"key1": "value3", "key2": "value4"},
    # more dictionaries
]

Example

  • Python

Python

students = [

   {"name": "Alice", "age": 21, "major": "Computer Science"},

   {"name": "Bob", "age": 22, "major": "Mathematics"},

   {"name": "Charlie", "age": 20, "major": "Physics"}

]

print(students)
You can also try this code with Online Python Compiler
Run Code


Output:

[{'name': 'Alice', 'age': 21, 'major': 'Computer Science'}, {'name': 'Bob', 'age': 22, 'major': 'Mathematics'}, {'name': 'Charlie', 'age': 20, 'major': 'Physics'}]

Accessing Data in a List of Dictionaries

Accessing Individual Dictionaries

To access a dictionary within the list, use indexing:

python

  • Python

Python

first_student = students[0]

print(first_student)
You can also try this code with Online Python Compiler
Run Code

Output:

{'name': 'Alice', 'age': 21, 'major': 'Computer Science'}

Accessing Values Using Keys

To access values within a dictionary, use the key:

  • Python

Python

alice_name = students[0]["name"]

print(alice_name)
You can also try this code with Online Python Compiler
Run Code


Output:

Alice

Adding and Removing Elements

Adding a Dictionary to the List

To add a new dictionary to the list, use the append method:

  • Python

Python

new_student = {"name": "David", "age": 23, "major": "Chemistry"}

students.append(new_student)

print(students)
You can also try this code with Online Python Compiler
Run Code


Output

[{'name': 'Alice', 'age': 21, 'major': 'Computer Science'}, {'name': 'Bob', 'age': 22, 'major': 'Mathematics'}, {'name': 'Charlie', 'age': 20, 'major': 'Physics'}, {'name': 'David', 'age': 23, 'major': 'Chemistry'}]

Removing a Dictionary from the List

To remove a dictionary, use the remove method or the del keyword:

  • Python

Python

students.remove({"name": "Bob", "age": 22, "major": "Mathematics"})

print(students)
You can also try this code with Online Python Compiler
Run Code


Output

[{'name': 'Alice', 'age': 21, 'major': 'Computer Science'}, {'name': 'Charlie', 'age': 20, 'major': 'Physics'}, {'name': 'David', 'age': 23, 'major': 'Chemistry'}]

Modifying Dictionaries in a List

Updating Values

To update a value in a dictionary:

  • Python

Python

students[0]["age"] = 22

print(students[0])
You can also try this code with Online Python Compiler
Run Code


Output:

{'name': 'Alice', 'age': 22, 'major': 'Computer Science'}

Adding New Key-Value Pairs

To add a new key-value pair to a dictionary:

  • Python

Python

students[1]["GPA"] = 3.8

print(students[1])
You can also try this code with Online Python Compiler
Run Code


Output:

{'name': 'Charlie', 'age': 20, 'major': 'Physics', 'GPA': 3.8}

Removing Key-Value Pairs

To remove a key-value pair:

  • Python

Python

del students[2]["major"]

print(students[2])
You can also try this code with Online Python Compiler
Run Code


Output

{'name': 'David', 'age': 23}

Sorting a List of Dictionaries

Sorting by a Key

To sort the list of dictionaries by a specific key:

  • Python

Python

students_sorted_by_age = sorted(students, key=lambda x: x["age"])

print(students_sorted_by_age)
You can also try this code with Online Python Compiler
Run Code


Output:

[{'name': 'Charlie', 'age': 20, 'major': 'Physics', 'GPA': 3.8}, {'name': 'Alice', 'age': 22, 'major': 'Computer Science'}, {'name': 'David', 'age': 23}]

Frequently Asked Questions

How do I update a specific dictionary in a list?

You can access the dictionary by its index in the list and then update the key-value pair.

How can I remove a dictionary from a list based on a condition?

Use a list comprehension to create a new list without the unwanted dictionary:

students = [student for student in students if student["name"] != "Bob"]

Can I sort a list of dictionaries by multiple keys?

Yes, use the sorted function with a lambda function that returns a tuple of keys.

Conclusion

A list of dictionaries in Python is a powerful and flexible data structure that allows you to store and manipulate complex data. By understanding how to create, access, modify, and perform common operations on a list of dictionaries, you can efficiently manage data in your Python programs. Remember to follow best practices and use the right methods for your specific use cases to ensure your code is both efficient and readable.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass