Lists are the most basic containers that are built into the Python programming language. Because lists do not always have to be uniform, it is the most powerful tool in Python. A list can contain Data Types such as Integer, String, and Object. Lists are mutable, which means they can be changed even after they are created. In this tutorial, we will talk about how to make a list of lists in Python.
What are lists of lists in Python?
In Python, a list of lists is a list with lists as its items. Here is an illustration of a list of lists:
myList=[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
Here, myList contains five lists as its members. Hence, it is a list of lists.
Creating a List of Lists Python
In Python, you can use square brackets to group all the lists together to make a list of lists. For instance, you can enclose lists in square brackets as demonstrated in the following Python code if you have five lists and wish to generate a list of lists from the provided lists.
list1 = [1, 2, 3, 4, 5] print("The first list is:", list1) list2 = [12, 13, 23] print("The second list is:", list2) list3 = [10, 20, 30] print("The third list is:", list3) list4 = [11, 22, 33] print("The fourth list is:", list4) list5 = [12, 24, 36] print("The fifth list is:", list5) myList = [list1, list2, list3, list4, list5] print("The list of lists is:") print(myList)
Output:
The first list is: [1, 2, 3, 4, 5] The second list is: [12, 13, 23] The third list is: [10, 20, 30] The fourth list is: [11, 22, 33] The fifth list is: [12, 24, 36] The list of lists is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
You can see from the example above that we used the provided lists to generate a list of lists.
List List Using the append() method in Python
Using Python’s append() method, we can also produce a list of lists. When used on a list, the append() function adds an object as input to the end of the list. We will first construct a new empty list before utilizing the add() method to create a list of lists. You can do this by using the list() constructor or the square bracket notation. When the list() constructor is used without any input arguments, an empty list is produced.
The following Python code demonstrates how to generate a list of lists using the add() method after establishing an empty list. This method creates a list of lists in Python.
list1 = [1, 2, 3, 4, 5] print("The first list is:", list1) list2 = [12, 13, 23] print("The second list is:", list2) list3 = [10, 20, 30] print("The third list is:", list3) list4 = [11, 22, 33] print("The fourth list is:", list4) list5 = [12, 24, 36] print("The fifth list is:", list5) myList = [] myList.append(list1) myList.append(list2) myList.append(list3) myList.append(list4) myList.append(list5) print("The list of lists is:") print(myList)
Output:
The first list is: [1, 2, 3, 4, 5] The second list is: [12, 13, 23] The third list is: [10, 20, 30] The fourth list is: [11, 22, 33] The fifth list is: [12, 24, 36] The list of lists is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
Using only integer data types, you may use nested for loops and the append() method to create a list of lists, simulating a 2-dimensional array.
In this method, we’ll first make a new list, say, called myList. The other lists will then be added to myList using a nested for loop. Another empty list, such as tempList, will be created and placed in the nested loop’s outer for loop. Therefore, using the append() method, we will concatenate integers to tempList in the inner for loop.
We will obtain a list of integers after appending the numbers to tempList. The outer for loop will be reached after that, and tempList will be added to myList. In this manner, a list of lists can be made.
Let’s say we need to generate a 3×3 array of numbers as an example. To create a list of lists in Python, we will do it by using the range() function and a for loop.
myList = [] for i in range(3): tempList = [] for j in range(3): element = i + j tempList.append(element) myList.append(tempList) print("The list of lists is:") print(myList)
Output:
The list of lists is: [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
Accessing elements in a list of lists in Python
Using the list index, we may retrieve the list’s contents. We can directly access the list elements in a flat list or a 1-dimensional list using the element’s index. For instance, we can access the first item of the list using index 0, as shown below, if we want to utilize positive values as indexes for list components.
myList = [1, 2, 3, 4, 5] print("The list is:") print(myList) print("The first item of the list is:") print(myList[0])
Output:
The list is: [1, 2, 3, 4, 5] The first item of the list is: 1
Similar to how we can get the last member of the list using index -1 if we use negative values faor the list index, see the example below.
myList = [1, 2, 3, 4, 5] print("The list is:") print(myList) print("The last item of the list is:") print(myList[-1])
Output:
The list is: [1, 2, 3, 4, 5] The last item of the list is: 5
CONCLUSION
We covered the list of lists in Python in this article. We’ve talked about the many operations that can be done on a list of lists. If you’re interested in the Python content of lists. We have everything you need right here.
At ONEXT DIGITAL, we implements a team of experts and experienced developers to provide high-quality Mobile Application Development services. We will give you the highest level of satisfaction.
>> Read also:
Python Pros And Cons: Main Points You Need To Know (2023 Update)