How to create List of Lists in Python

Hi guys. How are you all? I hope you all fine. Today in this tutorial we will learn about How to create List of Lists in Python. Here we will learn about of the different methods which will combine different two or ore list into one list. You can also repeat the same list so many times and make the list of these lists. So lets learn about of these method. I hope you guys like it.

How to create List of Lists in Python

  1. Create List of Lists in Python

    to create List of Lists in Python just Use append(). By using append you can combine two or more list into one list. Here we will learn how to make a list of lists. Here i am using three different lists to combine it into one list. I am taking lst1, lst2 and lst3 and then using the append() function to combine it into one list and then printing it. You can see in my output the combined list. Lets learn this by below example. I hope you guys like it. lst1 = [2,5,8] lst2 = [8,4,6] lst3 = [10,5,4] lst = [] lst.append(lst1) lst.append(lst2) lst.append(lst3) print(lst) Output : [[2, 5, 8], [8, 4, 6], [10, 5, 4]]

  2. How to create List of Lists in Python

    to create List of Lists in Python just Use for loop. By using for loop you can create list of lists. For this you have to use for loop with append() and you have to set the range of the list. Here you can repeat a list so many times in one single list. here the range is the number you want to repeat it. So lets learn about of this method by given below example: lst = [] for i in range(4): lst.append([]) for j in range(3): lst[i].append(j) print(lst) Output : [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]

Method 1: Use append()

By using append you can combine two or more list into one list. Here we will learn how to make a list of lists. Here i am using three different lists to combine it into one list. I am taking lst1, lst2 and lst3 and then using the append() function to combine it into one list and then printing it. You can see in my output the combined list. Lets learn this by below example. I hope you guys like it.

lst1 = [2,5,8]
lst2 = [8,4,6]
lst3 = [10,5,4]
lst = []
lst.append(lst1)
lst.append(lst2)
lst.append(lst3)
print(lst)

Output :

[[2, 5, 8], [8, 4, 6], [10, 5, 4]]

Method 2: Use for loop

By using for loop you can create list of lists. For this you have to use for loop with append() and you have to set the range of the list. Here you can repeat a list so many times in one single list. here the range is the number you want to repeat it. So lets learn about of this method by given below example:

lst = [] 
for i in range(4):
    lst.append([]) 
    for j in range(3): 
        lst[i].append(j)  
print(lst) 

Output :

[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]

Method 3: Use list comprehension

You can use list comprehension to create a list of lists. Here you can repeat a list so many times by using the for loop and can get the list of these same lists. Lets learn this by given example:

lst1 = [51,42,62]
lst = [lst1 for i in range(3)]
print(lst)

Output :

[[51, 42, 62], [51, 42, 62], [51, 42, 62]]

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment