Create a Nested List in Python

A nested list in Python is a list that contains other lists as its elements. This structure allows you to create multi-dimensional lists, useful for representing tables, matrices, or hierarchical data. You can create a nested list using square brackets [], and access elements using multiple indices.


Examples

1. Creating a Simple Nested List

We can create a nested list by defining a list where each element is itself a list.

</>
Copy
# Creating a nested list
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Printing the nested list
print("Nested List:", nested_list)

# Accessing an element from the nested list
print("Element at row 2, column 3:", nested_list[1][2])

Explanation

Here, nested_list is a list containing three inner lists, each representing a row in a matrix-like structure:

  • nested_list[0] = [1, 2, 3] (First row)
  • nested_list[1] = [4, 5, 6] (Second row)
  • nested_list[2] = [7, 8, 9] (Third row)

We access an individual element using two indices: nested_list[1][2] retrieves 6 (second row, third column).

Output:

Nested List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Element at row 2, column 3: 6

2. Creating a Nested List Using List Comprehension

We can use list comprehension to create a nested list dynamically.

</>
Copy
# Creating a nested list using list comprehension
nested_list = [[j for j in range(1, 4)] for i in range(3)]

# Printing the nested list
print("Nested List:", nested_list)

Explanation

In this example:

  • The outer loop runs 3 times (for i in range(3)), creating 3 sublists.
  • The inner loop generates a list of values [1, 2, 3] in each iteration.
  • This results in a structure similar to a 3×3 matrix.

Output:

Nested List: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

3. Creating a Nested List with Different Data Types

A nested list can contain mixed data types, including strings, numbers, and booleans.

</>
Copy
# Creating a nested list with mixed data types
data = [
    ["Alice", 25, True],
    ["Bob", 30, False],
    ["Charlie", 22, True]
]

# Printing the nested list
print("Nested List:", data)

# Accessing an element (Charlie's age)
print("Charlie's age:", data[2][1])

Explanation

Here, data is a nested list where each inner list represents a person with:

  • A name (string)
  • An age (integer)
  • A boolean value (e.g., True for active status)

We access Charlie’s age using data[2][1], which retrieves 22.

Output:

Nested List: [['Alice', 25, True], ['Bob', 30, False], ['Charlie', 22, True]]
Charlie's age: 22

4. Creating a Nested List Using the append() Method

We can create a nested list dynamically by using the append() method.

</>
Copy
# Creating an empty list
nested_list = []

# Adding sublists dynamically
nested_list.append([10, 20, 30])
nested_list.append([40, 50, 60])
nested_list.append([70, 80, 90])

# Printing the nested list
print("Nested List:", nested_list)

Explanation

We start with an empty list nested_list and add sublists using append(). Each call to append() inserts a new inner list.

Output:

Nested List: [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

Conclusion

Nested lists are useful for organizing hierarchical data structures. The methods covered include:

  1. Manually defining a nested list.
  2. Using list comprehension to create dynamic structures.
  3. Storing mixed data types in a nested list.
  4. Using the append() method to build nested lists dynamically.