Create A List of Lists Using For Loop
Last Updated :
28 Apr, 2025
In Python, creating a list of lists using a for loop involves iterating over a range or an existing iterable and appending lists to the main list. This approach allows for the dynamic generation of nested lists based on a specific pattern or condition, providing flexibility in list construction. In this article, we will explore four different approaches to creating a list of lists using got loop in Python.
Create List of Lists in Python
Below are some of the ways by which we can create a list of lists using for loop in Python:
Create List of Lists Using for Loop with List Comprehension
The below code initializes an empty list called listOfList and, using a for loop with list comprehension generates a list of lists where each inner list contains three consecutive elements, resulting in listOfList representing a matrix with three rows. The print(listOfList) statement outputs the matrix structure.
Python
# initialise the empty list to store list of list
listOfList = []
for row in range(0, 3):
# for each row generate a list which contain the 3 elements in it
# generated using list comprehension
rowList = [element for element in range(row, row+3)]
# put the rowList in the list of list
listOfList.append(rowList)
# displaying the store values in listOfLits
print(listOfList)
Output[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
Create List of Lists Using for Loop with append() Function
The below code initializes an empty list called listOfList and, using a nested for loop with the append() method generates a list of lists. Each inner list corresponds to a row, and the elements in each row are integers from 0 to the row number. The final result is displayed by printing each inner list within listOfList.
Python
# initialise an empty list which will store the list of list
listOfList = []
# this will create the number of rows
for row in range(0, 3):
# for each row create an empty list to store the elements of that row
rowList = []
for column in range(row+1):
rowList.append(column)
# once we have complete the row list
# we can put it into the original list of list
listOfList.append(rowList)
# displaying the store values in
for eachList in listOfList:
print(eachList)
Output[0]
[0, 1]
[0, 1, 2]
Create List of Lists Using for Loop with zip() Function
The code initializes an empty list called listOfList
and uses a for loop with the zip()
function to iterate over corresponding elements of three lists (list1
, list2
, and list3
). For each iteration, a rowList
is created by combining the corresponding elements, and this rowList
is appended to listOfList
. The resulting list of lists is displayed using print(listOfList).
Python
# initialise the empty list of store the list of list
listOfList = list()
# create list which will be passed as parameter in the zip
list1 = [1, 4, 7]
list2 = [2, 5, 8]
# this list has one extra item which will be dropped
list3 = [3, 6, 9, 10]
# iterate on the corresponding element in each list
for element1, element2, element3 in zip(list1, list2, list3):
# create a list by using corresponding element from each list
rowList = list([element1, element2, element3])
# append the rowList in the main list
listOfList.append(rowList)
# Display the output
print(listOfList)
Output[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Conclusion
In conclusion, creating a list of lists in Python using a for loop offers versatile methods for dynamic list generation. Whether utilizing list comprehension, the append() function, nested list comprehension, or the zip() function within a loop, these approaches provide flexibility and efficiency in constructing nested structures.
Similar Reads
Python - Create list of tuples using for loop In this article, we will discuss how to create a List of Tuples using for loop in Python. Let's suppose we have a list and we want a create a list of tuples from that list where every element of the tuple will contain the list element and its corresponding index. Method 1: Using For loop with append
2 min read
Creating A New List For Each For Loop In Python, creating new lists inside a for loop is an approach that is commonly used to manage and process data during iterations. This allows us to generate new lists for each loop iteration, ensuring that data remains isolated and manageable. In this article, weâll explore how to create new lists
2 min read
Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
How to Create List of Dictionary in Python Using For Loop The task of creating a list of dictionaries in Python using a for loop involves iterating over a sequence of values and constructing a dictionary in each iteration. By using a for loop, we can assign values to keys dynamically and append the dictionaries to a list. For example, with a list of keys a
3 min read
Python - Create a List of Floats Creating a list of floats in Python can be done in several simple ways. The easiest way to create a list of floats is to define the numbers directly. Hereâs how we can do it:Pythona = [0.1, 2.3, 4.5, 6.7] print(a) Output[0.1, 2.3, 4.5, 6.7] Using List ComprehensionList comprehension is a shorter way
1 min read
How to Input a List in Python using For Loop Using a for loop to take list input is a simple and common method. It allows users to enter multiple values one by one, storing them in a list. This approach is flexible and works well when the number of inputs is known in advance.Letâs start with a basic way to input a list using a for loop in Pyth
2 min read
Flatten a List of Lists in Python Flattening a list of lists means turning a nested list structure into a single flat list. This can be useful when we need to process or analyze the data in a simpler format. In this article, we will explore various approaches to Flatten a list of Lists in Python.Using itertools.chain itertools modul
3 min read
Iterate Over a List of Lists in Python We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest
2 min read
Python - Convert list of strings and characters to list of characters Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Using itertools.chain()itertools.chain() combines multiple lists or iterables i
3 min read