← Back to Blog

Going Deeper with Nested Lists

The problem...

You have three squads. Each squad has its own list of members.

You could create three separate lists:

alpha = ["Raven", "Wolf", "Ghost"]
bravo = ["Viper", "Bull", "Fox"]
delta = ["Hawk", "Stone", "Cruz"]

But what if you want to work with all of them together? Pass them around? Loop through all squads at once?

Three separate variables don't scale. You need one structure that holds all three.

The idea!

A list can hold any value — including other lists.

A list that contains lists is called a nested list.

One outer list. Multiple inner lists. Each one fully functional.

Making it real

squads = [
    ["Raven", "Wolf", "Ghost"],
    ["Viper", "Bull", "Fox"],
    ["Hawk", "Stone", "Cruz"]
]
print(squads)

Output → [['Raven', 'Wolf', 'Ghost'], ['Viper', 'Bull', 'Fox'], ['Hawk', 'Stone', 'Cruz']]

Three lists inside one. Each inner list is one element of the outer list.

Accessing elements

You use two indexes — one for the outer list, one for the inner list:

squads = [
    ["Raven", "Wolf", "Ghost"],
    ["Viper", "Bull", "Fox"],
    ["Hawk", "Stone", "Cruz"]
]

print(squads[0])        # first inner list
print(squads[0][1])     # second element of first inner list

Output:

['Raven', 'Wolf', 'Ghost']
Wolf

squads[0] gets you the first inner list. squads[0][1] gets you the element at index 1 inside that list.

Looping through a nested list

One loop gives you each inner list. A second loop goes inside it:

squads = [
    ["Raven", "Wolf", "Ghost"],
    ["Viper", "Bull", "Fox"],
    ["Hawk", "Stone", "Cruz"]
]

for squad in squads:
    for member in squad:
        print(member)

Output:

Raven
Wolf
Ghost
Viper
Bull
Fox
Hawk
Stone
Cruz

The outer loop moves through squads. The inner loop moves through each squad's members.

A real use case — a grid

Nested lists are the natural way to represent a grid — rows and columns:

grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

print(grid[1][2])       # row 1, column 2

Output → 6

Row first, column second. That's the convention.

Modifying nested lists

The same mutating methods work on inner lists — you just need to reach them first:

squads = [
    ["Raven", "Wolf", "Ghost"],
    ["Viper", "Bull", "Fox"]
]

squads[0].append("Cruz")
print(squads[0])

Output → ['Raven', 'Wolf', 'Ghost', 'Cruz']

What's really happening

Each inner list is a full list — it has its own elements, its own methods, its own length.

The outer list just holds references to them.

When you write squads[0], Python returns the first inner list. Then [1] indexes into that.

Heads up!

  • Each inner list can have a different length — Python doesn't enforce uniformity
  • len(squads) gives you the number of inner lists — not the total number of elements
  • Indexes go from outside in — outer first, inner second
  • Nesting more than two levels deep gets hard to read fast — keep it shallow when you can

The mindset shift

Stop thinking: "I need a separate variable for each group."

Start thinking: "Groups of data belong in a list of lists — one structure, full control."

What you should understand now

  • A list can contain other lists
  • Use two indexes to access an element — outer list first, inner list second
  • Loop with a nested for to reach every element
  • Inner lists are fully functional — all methods work on them
  • len() on the outer list counts inner lists, not total elements
[ login to bookmark ] // copied! 19 views · 2 min
// resources
Code Example list_nested.py
← prev Going Through Every Element next → Lists Mini Project — Shopping List
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.