Accessing Data in a List of Dictionaries
Accessing Individual Dictionaries
To access a dictionary within the list, use indexing:
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
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
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
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
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
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
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
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 Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.