Python Nested Lists: How to Create, Access, and Update

Python lists can contain not only numbers and strings but also other lists as elements. This structure, where a list is inside another list, is called a “Nested List”.

Nested lists are very useful for representing 2-dimensional grids (matrices), coordinate data, or grouped data (e.g., subject scores for each student).

This article explains the basics of creating, accessing, and updating nested lists.


目次

Creating Nested Lists

You can create nested lists using [] (square brackets) just like normal lists. Simply place comma-separated lists inside the outer [].

For example, let’s consider test score data for 3 students across 3 subjects (Japanese, Math, English).

# Create a list for [Japanese, Math, English] scores
# and group them by student (for 3 students) in a nested list
student_scores = [
    [80, 90, 85],  # 1st Student (Index 0)
    [92, 88, 95],  # 2nd Student (Index 1)
    [78, 85, 80]   # 3rd Student (Index 2)
]

print(f"All Scores: {student_scores}")

Output:

All Scores: [[80, 90, 85], [92, 88, 95], [78, 85, 80]]

Accessing Elements in a Nested List

To access elements in a nested list, use the index reference [] twice (or more).

Syntax: my_list[index1][index2]

  • [index1]: Index of the outer list (Which inner list?)
  • [index2]: Index of the inner list (Which element inside it?)

1. Accessing an Entire Inner List

If you specify only one index, you retrieve the entire inner list at that position.

# Get the entire score list for the 2nd student (Index 1)
scores_student_2 = student_scores[1]

print(f"2nd Student Scores: {scores_student_2}")

Output:

2nd Student Scores: [92, 88, 95]

2. Accessing a Specific Element

By specifying the index twice, you can directly access a specific element inside the inner list.

# Get the score for the 3rd subject (English, Index 2)
# for the 2nd student (Index 1)
score_student_2_english = student_scores[1][2]

print(f"2nd Student's English Score: {score_student_2_english}")

# Get the score for the 1st subject (Japanese, Index 0)
# for the 1st student (Index 0)
score_student_1_japanese = student_scores[0][0]

print(f"1st Student's Japanese Score: {score_student_1_japanese}")

Output:

2nd Student's English Score: 95
1st Student's Japanese Score: 80

Updating Elements in a Nested List

To update an element in a nested list, specify the index twice and assign (=) the new value, just like accessing it.

print(f"Before: {student_scores}")

# Change the 2nd subject (Math, Index 1) score
# for the 1st student (Index 0) from 90 to 93
student_scores[0][1] = 93

print(f"After:  {student_scores}")

Output:

Before: [[80, 90, 85], [92, 88, 95], [78, 85, 80]]
After:  [[80, 93, 85], [92, 88, 95], [78, 85, 80]]

You can see that the value at student_scores[0][1] has been updated from 90 to 93.


Summary

  • A Nested List is a structure where a list holds other lists as elements.
  • Use my_list[index1] to access the entire inner list.
  • Use my_list[index1][index2] to access or update a specific element inside.
  • Mastering the use of two indices like [][] is key to working with 2-dimensional data.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次