Creating a list of lists in python is very versatile and helps you manipulate data more efficiently.

In python, we can use multiple data types including list to hold multiple values in one single variable. It’s important to know that each element of the outer list, i.e., sublist, could be a separate …

Updated October 12, 2023

In python, we can use multiple data types including list to hold multiple values in one single variable. It’s important to know that each element of the outer list, i.e., sublist, could be a separate list. This feature allows for complex data structures in python programming.

To create a list of lists in Python is quite simple and straightforward. Here is how you would do it:

First, define a list:

my_list = [1,2,3]
print(my_list)   # Outputs: [1, 2, 3]

Next, declare the list of lists:

my_lists = [[1,2,3], [4,5,6]]
print(my_lists)  # Outputs: [[1, 2, 3], [4, 5, 6]]

In this example, my_list and my_lists are both lists. The first element of my_lists is the list [1,2,3], while the second element is a new list [4,5,6].

You can access individual elements like this:

print(my_lists[0])   # Outputs: [1, 2, 3]
print(my_lists[1])   # Outputs: [4, 5, 6]

The second list (my_lists[1]) is a list of its own, making my_lists essentially a ‘multi-dimensional’ list. This feature can be incredibly useful when dealing with complex data structures and tasks such as matrix operations in numerical computation or graph algorithms.

Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!