Read Also: Convert list to set in Python
What is a list of lists in Python?
A list of lists in Python can be defined as a list object where each list element is a list by itself as shown below in the diagram.
How to make a list of lists in Python?
It is quite simple to create a list of lists in Python. Use Python list's append() method to create a list of lists as shown in the below example.
list1 = [10, 20, 30] list2 = [40, 50, 60] listoflists = [] listoflists.append(list1); listoflists.append(list2); print ("list of lists: ", listoflists);
Output:
list of lists: [[10, 20, 30], [40, 50, 60]]
How to print a list of lists without brackets in Python:
We can print a list of lists without brackets by using for loop and print statement as shown in the below example:
listoflists = [[10, 20, 30], [40, 50, 60]] for i in listoflists: print(*i)
Output:
10 20 30
40 50 60
How to access elements of a list of lists in Python:
As we know index starts from 0. To access the elements of a list of lists, we need to provide an index of the list from a list of lists and after that index of the element, you want to access in the list.
For example: if we want to access element 50 from list of lists then you need to provide listoflists[1][1] as shown below in the code:
listoflists = [[10, 20, 30], [40, 50, 60]] print("Access element in List of Lists:",listoflists[1][1])
Output:
Access element in List of Lists: 50
That's all for today, please mention in the comments in case you have any questions related to how to make a list of lists in Python.