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!

Creating a List of Lists in Python

Learn how to create and work with lists of lists in Python, a fundamental concept in programming. …


Updated July 27, 2023

Learn how to create and work with lists of lists in Python, a fundamental concept in programming.

How to Create a List of Lists in Python

Definition of the Concept


In Python, a list of lists is also known as a nested list or a 2D list. It’s a data structure that consists of multiple lists, where each inner list is an element of the outer list. Think of it like a matrix, where each row is a separate list.

Step-by-Step Explanation


Creating a list of lists in Python is straightforward. You can use the following syntax:

list_of_lists = [[element1], [element2], ...]

Let’s break down this example:

  • list_of_lists is the name we give to our nested list.
  • The first set of square brackets [] defines the outer list, which will contain multiple inner lists.
  • Each inner list is defined within its own set of square brackets [element1], [element2].
  • element1, element2, etc. are the actual elements that make up each inner list.

Here’s a simple example:

fruits = [["Apple"], ["Banana"], ["Cherry"]]
print(fruits)

Output:

[['Apple'], ['Banana'], ['Cherry']]

In this example, fruits is the outer list, and ["Apple"], ["Banana"], and ["Cherry"] are each inner lists.

Working with Lists of Lists

Once you have a list of lists, you can access individual elements using standard indexing techniques. For example:

fruits = [["Apple"], ["Banana"], ["Cherry"]]
print(fruits[0])  # Output: ['Apple']
print(fruits[1][0])  # Output: 'Banana'

In the first example, we access the entire inner list at index 0. In the second example, we use double indexing to access the single element 'Banana' within the inner list.

Nested List Operations


You can perform various operations on nested lists, such as:

  • Append: Add a new inner list to the end of the outer list using the append() method.
  • Insert: Insert a new inner list at a specific position within the outer list using the insert() method.
  • Modify elements: Update individual elements within an inner list using standard indexing techniques.

Here are some examples:

fruits = [["Apple"], ["Banana"], ["Cherry"]]
fruits.append(["Date"])
print(fruits)  # Output: [['Apple'], ['Banana'], ['Cherry'], ['Date']]

fruits.insert(0, ["Apricot"])
print(fruits)  # Output: [['Apricot'], ['Apple'], ['Banana'], ['Cherry'], ['Date']]

fruits[1][0] = "Blueberry"
print(fruits)  # Output: [['Apricot'], ['Blueberry'], ['Banana'], ['Cherry'], ['Date']]

Conclusion


Creating a list of lists in Python is a fundamental concept that opens up a world of possibilities for data manipulation and analysis. By understanding how to create, access, and modify nested lists, you can write more efficient and effective code that takes advantage of Python’s built-in data structures.

I hope this article has provided a comprehensive guide to creating and working with lists of lists in Python. If you have any questions or need further clarification on any of the concepts discussed here, please don’t hesitate to ask!

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

Intuit Mailchimp