Featured Article

Master Python List Nested in Dictionary: A Comprehensive Guide

Nicole Jul 13, 2026

Python, a high-level and interpreted programming language, is renowned for its simplicity and efficiency. One of the powerful data structures in Python is the list, offering dynamic arrays with flexible sizes. But what if you want to store more complex data structures within a list? Here's where Python dictionaries step in, allowing you to nest lists within dictionaries for intricate and organized data manipulation.

Python Dictionaries Explained (With Real Examples)
Python Dictionaries Explained (With Real Examples)

Imagine you're building an application that tracks students' scores in various subjects. Each student has scores in multiple subjects, which can be represented as a list of subject scores. However, you also need to manage different students' data simultaneously. Python dictionaries come to the rescue, providing a way to nest these lists, thus storing and manipulating data elegantly.

Python Collection Data Types | Lists, Tuples, Sets & Dictionaries Explained
Python Collection Data Types | Lists, Tuples, Sets & Dictionaries Explained

Understanding Nested Lists in Dictionaries

In Python, you can nest a list inside a dictionary by assigning a list as a value to a dictionary key. Here's a simple example:

Python important methods - Programming
Python important methods - Programming

```python student_scores = {"Maths": [85, 90, 92], "Science": [88, 91, 89]} ```

In the above nested list in a dictionary, 'Maths' and 'Science' are keys, while the lists [85, 90, 92] and [88, 91, 89] are their corresponding values. These lists represent the scores of a student in 'Maths' and 'Science' subjects respectively.

Accessing Nested Lists

the cover of collection data types in python, with an image of numbers and symbols
the cover of collection data types in python, with an image of numbers and symbols

To access the nested lists, you'll first need to access the dictionary key, then the list index. Here's how:

```python print(student_scores["Maths"][1]) ```

This will output '90', the second score in the 'Maths' subject.

Modifying Nested Lists

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

Similarly, you can modify the values in the nested lists. Here's how:

```python student_scores["Maths"][2] = 95 ```

This changes the third score in the 'Maths' subject from 92 to 95.

Remember, the scoring system for the index in the list starts from 0. So, 'Maths'[2] refers to the third score.

python
python

Working with Nested Lists in a Loop

Sometimes, you might need to iterate through the dictionary to access and manipulate the nested lists. For instance, you can calculate the average score for each subject or student.

a black background with colorful text that says,'python dictionaries cheat sheet '
a black background with colorful text that says,'python dictionaries cheat sheet '
How To Flatten a Dictionary With Nested Lists and Dictionaries in Python
How To Flatten a Dictionary With Nested Lists and Dictionaries in Python
a poster with the words, what is list slicing in python? and how to use it
a poster with the words, what is list slicing in python? and how to use it
Data Type in Python ✌️
Data Type in Python ✌️
List vs Tuple vs Set vs Dictionary in Python (Beginner's Guide)
List vs Tuple vs Set vs Dictionary in Python (Beginner's Guide)
the data structures in python are displayed on a black background with red and green symbols
the data structures in python are displayed on a black background with red and green symbols
a screenshot of a computer screen with the text looping through dictionaries in python
a screenshot of a computer screen with the text looping through dictionaries in python
python variables
python variables
a poster with the words looping through nested lists in python
a poster with the words looping through nested lists in python
JSON & Python Data Types: Complete Mapping Guide | API Development
JSON & Python Data Types: Complete Mapping Guide | API Development
Python Basics Cheat Sheet for Beginners
Python Basics Cheat Sheet for Beginners
Python List Comprehension Cheat Sheet 📌
Python List Comprehension Cheat Sheet 📌
Data Structure in Python
Data Structure in Python
Python Dictionary Methods
Python Dictionary Methods
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Syntax Data Collections
Python Syntax Data Collections
10 Delicious Keto Recipes
10 Delicious Keto Recipes
🐍 Python Cheat Sheet: 8 One-Liners That Save Time
🐍 Python Cheat Sheet: 8 One-Liners That Save Time
Dictionaries in Python 💻
Dictionaries in Python 💻
Python Dictionary Methods Cheat Sheet for Beginners
Python Dictionary Methods Cheat Sheet for Beginners

Here's how you can calculate the average score for each subject:

```python for subject, scores in student_scores.items(): average_score = sum(scores) / len(scores) print(f"The average {subject} score is: {average_score}") ```

Iterating with 'enumerate()'

If you need the index while iterating, you can use the 'enumerate()' function:

```python for index, (subject, scores) in enumerate(student_scores.items()): print(f"At index {index}, the scores for {subject} are: {scores}") ```

This will output:

``` At index 0, the scores for Maths are: [85, 90, 92] At index 1, the scores for Science are: [88, 91, 89] ```

The index starts from 0, matching the zero-based indexing in Python.

Exploring and understanding how to manipulate nested lists in dictionaries is an exciting journey in Python. It opens up a world of possibilities for Managing complex data structures efficiently. And that's just a start! Python'scapabilities extend far beyond, providing a plethora of feature's to make data manipulation and analysis a breeze.