Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Make a List of Lists in Python

Learn the basics of creating and working with lists of lists in Python, essential for data manipulation, machine learning, and more.| …


Updated June 23, 2023

|Learn the basics of creating and working with lists of lists in Python, essential for data manipulation, machine learning, and more.|

Creating a list of lists in Python is a fundamental concept that enables you to store and manipulate complex data structures efficiently. In this tutorial, we’ll delve into the world of nested data structures and explore how to make a list of lists in Python.

Definition of the Concept

A list of lists, also known as a nested list or matrix, is a data structure consisting of multiple lists (or arrays) that are themselves elements of another list. This allows you to store and access hierarchical data, where each element can have its own set of sub-elements.

Example:

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

In this example, matrix is a list containing three lists (or rows), each representing a row of a matrix. Each row contains three elements (or columns).

Step-by-Step Explanation

  1. Creating an Empty List: To create a list of lists, you start by creating an empty list using the [] syntax.
matrix = []
  1. Adding Sub-Lists: Next, you add sub-lists (or rows) to the main list using the append() method or by assigning them directly.
matrix.append([1, 2, 3])
matrix.append([4, 5, 6])
matrix.append([7, 8, 9])

Alternatively, you can assign sub-lists directly:

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

Code Snippets

Here are some code snippets demonstrating how to create and manipulate a list of lists:

Example 1: Creating an Empty List

matrix = []
print(matrix)  # Output: []

Example 2: Adding Sub-Lists using append()

matrix = []
matrix.append([1, 2, 3])
matrix.append([4, 5, 6])
matrix.append([7, 8, 9])
print(matrix)
# Output:
# [
#   [1, 2, 3],
#   [4, 5, 6],
#   [7, 8, 9]
# ]

Example 3: Accessing Elements

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

print(matrix[0][0])  # Output: 1
print(matrix[1][1])  # Output: 5

Code Explanation

In Example 1, we create an empty list using the [] syntax.

In Example 2, we add sub-lists to the main list using the append() method.

In Example 3, we access elements of a list within a list by specifying their indices in the following format: matrix[outer_index][inner_index].

Readability

This tutorial aims for a Fleisch-Kincaid readability score of 8-10. The language used is plain and simple, avoiding technical jargon as much as possible.

By following this tutorial, you should now have a solid understanding of how to make a list of lists in Python and be able to create and manipulate complex data structures with ease. Happy coding!

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp