Open In App

Create A List of Lists Using For Loop

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.




# 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.




# 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).




# 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.


Article Tags :