You are currently viewing Create List of Lists in Python

How to create a list of lists in Python? In Python, a list of lists is a nested list where the list contains multiple sub-lists. Each sub-list can have a different length and can contain any type of data, including other lists. To create a list of lists in Python, you can use simple square brackets.

Advertisements

Besides square brackets, you can also create a list of lists in Python using many ways, for example, by using the [] notation, append(), list comprehension, pop(), and del statement. In this article, I will explain how to create a list of lists by using all these methods with examples.

Related: You can convert list of tuples to list of lists in Python.

1. Quick Examples of Creating List of Lists

If you are in a hurry, below are some quick examples of creating a list of lists.


# Quick examples of creating list of lists

# Example 1: Create a list of lists
list1 = [3, 6, 9]
list2 = [12, 15, 18]
list3 = [10, 20, 30]
list_of_lists = [list1, list2, list3]

# Example 2: Create a list of lists 
# Using list comprehension
lists = [[i+j for i in range(4)] for j in range(3)]

# Example 3: Create a list of lists 
# Using append() function
list1 = [2, 4, 6, 8]
list2 = [3, 5, 7, 9]
list_of_lists = []
list_of_lists.append(list1)
list_of_lists.append(list2)

2. Create a List of Lists in Python

You can create a list of lists in Python using square brackets notation. First create individual list separately and use these as elements to create another list. The inner lists can be any size.

In the below example, first, created 3 inner lists (list1, list2 and list3). and then, create a list of lists by adding these lists as elements to another list using square brackets. Now, the list contains 3 sub-lists, each of which contains 3 elements.


# Initialize list
list1 = [3, 6, 9]
list2 = [12, 15, 18]
list3 = [10, 20, 30]

# Create a list of lists
list_of_lists = [list1, list2, list3]
print("After creating a list of lists:\n",list_of_lists)

Yields below output.

create python list lists

3. Create a List of Lists Using List Comprehension

You can also create a list of lists in Python using list comprehension, It will create a list of three inner lists, where each inner list will contain four elements.

In this example, Each inner list is created using another list comprehension, which generates four elements (i+j) for each value i in the range 0-3. The outer list comprehension runs three times, with j taking the values 0, 1, and 2.


# Create a list of lists 
# Using list comprehension
lists = [[i+j for i in range(4)] for j in range(3)]
print(lists)

Yields below output.


# Output:
[[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]

4. Create a List of Lists Using append() Function

You can also initialize a list of lists by appending lists to a list, the append() function in Python can be used to add an element to the list.

For example, first, initialize two lists called list1 and list2, then create a empty list called list_of_lists to store the sublists. Now, append list1 to list_of_lists using the append() function, then append list2 to list_of_lists which creates list of lists.


# Initialize lists 
list1 = [2, 4, 6, 8]
list2 = [3, 5, 7, 9]

# Create an empty list
list_of_lists = []

# Create a list of lists 
# Using append() function
list_of_lists.append(list1)
list_of_lists.append(list2)
print(list_of_lists)

Yields below output.


# Output:
[[2, 4, 6, 8], [3, 5, 7, 9]]

5. Create a List of Lists Using For Loop

You can also create a list of lists using a for loop. For example, the outer for-loop for i in range(3) iterates three times to create a list of three sublists. The inner for-loop for j in range(3) iterates three times to create each individual sublist, containing the elements i+j.


# Create list of lists 
# Using for-loop
lists = []
for i in range(3):
    tempList = []
    for j in range(3):
        element = i + j
        tempList.append(element)
    lists.append(tempList)
print(lists)

Yields below output.


# Output:
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

6. Remove a List from a List of Lists

Let’s quickly see an operation on a list of lists. You can remove a list from a list of lists in Python by using below example


# Initialize list
list_of_lists = [[2, 4, 6], [3, 5, 7], [1, 3, 5]]

# Remove a list from list of lists
list_of_lists.pop(1)
print(list_of_lists)

# Output:
# [[2, 4, 6], [1, 3, 5]]

Alternatively, you can also use the del statement to remove a specific sublist by its index.


# Using del statement
del list_of_lists[1]
print(list_of_lists)

# Output:
# [[2, 4, 6], [1, 3, 5]]

Frequently Asked Questions

How can I initialize a list of lists with a specific size?

You can use nested list comprehensions to initialize a list of lists with a specific size. For example, to create a 3×3 matrix.

How do I access elements in a list of lists?

You can access elements in a list of lists using indexing. For example, to access the element in the second row and third column of a 3×3 matrix.

Can I have lists of different lengths within a list of lists?

You can have lists of different lengths within a list of lists. Python allows you to create jagged arrays where sublists can have different lengths.

How do I find the length of a list of lists?

The length of a list of lists is the number of sublists it contains. You can use the len function.

Can I use list comprehension to create a list of lists with specific values?

You can use list comprehension to create a list of lists with specific values. For example, to create a 2×3 matrix filled with zeros.

Conclusion

In this article, I have explained how to create a list of lists in Python by using the [] notation, list comprehension, append() with examples. Based on your need, you can use any of these examples.

Happy Learning !!