Lists are used in python to store data when we need to access them sequentially. In this article, we will discuss how we can create a list of lists in python. We will also implement programs to perform various operations like sorting, traversing, and reversing a list of lists in python.
- What Is a List of Lists in Python?
- Create a List of Lists in Python
- Access Elements in a List of Lists in Python
- Traverse a List of Lists in Python
- Delete an Element From a List of Lists in Python
- Flatten List of Lists in Python
- Reverse List of Lists in Python
- Sort List of Lists in Python
- Concatenate Two Lists of Lists in Python
- Copy List of Lists in Python
- Conclusion
What Is a List of Lists in Python?
A list of lists in python is a list that contains lists as its elements. Following is an example 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 elements. Hence, it is a list of lists.
Create a List of Lists in Python
To create a list of lists in python, you can use the square brackets to store all the inner lists. For instance, if you have 5 lists and you want to create a list of lists from the given lists, you can put them in square brackets as shown in the following python code.
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]]
In the above example, you can observe that we have created a list of lists using the given lists.
List of Lists Using the append() Method in Python
We can also create a list of lists using the append()
method in python. The append()
method, when invoked on a list, takes an object as input and appends it to the end of the list. To create a list of lists using the append()
method, we will first create a new empty list. For this, you can either use the square bracket notation or the list()
constructor. The list()
constructor, when executed without input arguments, returns an empty list.
After creating the empty list, we can append all the given lists to the created list using the append()
method to create a list of lists in python as shown in the following code snippet.
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]]
If you want to create a list of lists like a 2-d array using only integer data types, you can use nested for loops with the append()
method to create a list of lists.
In this approach, we will first create a new list, say myList
. After that, we will use nested for loop to append other lists to myList
. In the outer for loop of the nested loop, we will create another empty list, say tempList
. In the inner for loop, we will append the numbers to tempList
using the append()
method.
After appending the numbers to tempList
, we will get a list of integers. After that, we will come to the outer for loop and will append tempList
to myList
. In this way, we can create a list of lists.
For instance, suppose that we have to create a 3×3 array of numbers. For this, we will use the range()
function and the for loop to create a list of lists in python as follows.
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]]
Create List of Lists Using List Comprehension in Python
Instead of using the for loop, you can use list comprehension with the range()
function to create a list of lists in a concise way as shown in the following example.
myList = [[i+j for i in range(3)] for j in range(3)]
print("The list of lists is:")
print(myList)
Output:
The list of lists is:
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
Access Elements in a List of Lists in Python
We can access the contents of a list using the list index. In a flat list or 1-d list, we can directly access the list elements using the index of the elements. For instance, if we want to use positive values as the index for the list elements, we can access the 1st item of the list using index 0 as shown below.
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
Similarly, if we use the negative values as the list indices, we can access the last element of the list using the index -1 as shown 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
If you want to access the inner lists from a list of lists, you can use list indices in a similar way as shown in the above example.
If you are using positive numbers as list indices, you can access the first inner list from the list of lists using index 0 as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The list of lists is:")
print(myList)
print("The first item of the nested list is:")
print(myList[0])
Output:
The list of lists is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The first item of the nested list is:
[1, 2, 3, 4, 5]
Similarly, if you are using negative numbers as list indices, you can access the last inner list from the list of lists as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The list of lists is:")
print(myList)
print("The last item of the nested list is:")
print(myList[-1])
Output:
The list of lists is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The last item of the nested list is:
[12, 24, 36]
To access the elements of the inner lists, you need to use double square brackets after the list name. Here, the first square bracket denotes the index of the inner list, and the second square bracket denotes the index of the element in the inner list.
For example, you can access the third element of the second list from the list of lists using square brackets as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The list of lists is:")
print(myList)
print("The third element of the second inner list is:")
print(myList[1][2])
Output:
The list of lists is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The third element of the second inner list is:
23
Traverse a List of Lists in Python
To traverse the elements of a list of lists, we can use a for a loop. To print the inner lists, we can simply iterate through the list of lists as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
for inner_list in myList:
print(inner_list)
Output:
[1, 2, 3, 4, 5]
[12, 13, 23]
[10, 20, 30]
[11, 22, 33]
[12, 24, 36]
Instead of printing the whole list while traversing the list of lists, we can also print the elements of the list. For this, we will use another for loop in addition to the for loop shown in the previous example. In the inner for loop, we will iterate through the inner lists and print their elements as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
for inner_list in myList:
for element in inner_list:
print(element, end=",")
print("")
Output:
1,2,3,4,5,
12,13,23,
10,20,30,
11,22,33,
12,24,36,
Delete an Element From a List of Lists in Python
To delete an inner list from a list of lists, we can use different methods of list-objects.
Delete an Element From a List of Lists Using the pop() Method
We can use the pop()
method to delete the last item from the list of lists. The pop()
method, when invoked on the list of lists, deletes the last element and returns the list present at the last position. We can understand this using a simple example shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
myList.pop()
print("The modified list is:")
print(myList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The modified list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33]]
To delete any other inner list, we will need to know its index. For instance, we can delete the second element of the list of lists using the pop()
method. For this, we will invoke the pop()
method on the list and will pass the index of the second list i.e. 1 to the pop()
method. After execution, the pop()
method will delete the second inner list from the list of lists and will return it as shown in the following example.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
myList.pop(1)
print("The modified list is:")
print(myList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The modified list is:
[[1, 2, 3, 4, 5], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
Remove an Element From a List of Lists Using the remove() Method
If we know the element that has to be deleted, we can also use the remove()
method to delete an inner list. The remove()
method, when invoked on a list, takes the element to be deleted as its input argument. After execution, it deletes the first occurrence of the element passed as the input argument. To delete any inner list, we can use the remove()
method as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
myList.remove([1, 2, 3, 4, 5])
print("The modified list is:")
print(myList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The modified list is:
[[12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
Flatten List of Lists in Python
Sometimes, we need to flatten a list of lists to create a 1-d list. To flatten the list of lists, we can use a for loop and the append()
method. In this approach, we will first create an empty list say outputList
.
After creating outputList
, we will use a nested for loop to traverse the list of lists. In the outer for loop, we will select an inner list. After that, we will traverse the elements of the inner list in the inner for loop. In the inner for loop, we will invoke the append()
method on outputList
and will pass the elements of the inner for loop as an input argument to the append()
method.
After execution of the for loops, we will get a flat list created from the list of lists as shown in the following code.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = []
for inner_list in myList:
for element in inner_list:
outputList.append(element)
print("The flattened list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The flattened list is:
[1, 2, 3, 4, 5, 12, 13, 23, 10, 20, 30, 11, 22, 33, 12, 24, 36]
Instead of using the for loop, you can also use list comprehension to flatten a list of lists as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = [x for l in myList for x in l]
print("The flattened list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The flattened list is:
[1, 2, 3, 4, 5, 12, 13, 23, 10, 20, 30, 11, 22, 33, 12, 24, 36]
Reverse List of Lists in Python
We can reverse a list of lists in two ways. One approach is to reverse the order of the inner lists only and leave the order of the elements in the inner lists intact. Another approach is to reverse the order of the elements in the inner lists too.
Reverse the Order of Inner List in List of Lists in Python
To simplify reverse the order of the inner lists, we will first create an empty list, say outputList
. After that, we will traverse through the list of lists in reverse order. While traversal, we will append the inner lists to outputList
. In this way, we will get the reversed list of lists outputList
after the execution of the for loop. You can observe this in the following example.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = []
listlen = len(myList)
for i in range(listlen):
outputList.append(myList[listlen - 1 - i])
print("The reversed list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The reversed list is:
[[12, 24, 36], [11, 22, 33], [10, 20, 30], [12, 13, 23], [1, 2, 3, 4, 5]]
Instead of using the for loop, you can use the reverse()
method to reverse the list of lists. The reverse()
method, when invoked on a list, reverses the order of the elements in the list. When we will invoke the reverse()
method on the list of lists, it will reverse the order of the inner lists as shown in the following example.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
myList.reverse()
print("The reversed list is:")
print(myList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The reversed list is:
[[12, 24, 36], [11, 22, 33], [10, 20, 30], [12, 13, 23], [1, 2, 3, 4, 5]]
In the above approach, the original list is modified. However, that wasn’t the situation in the previous example. Thus, you can choose an approach depending on whether you have to modify the original list or not.
Reversing the Order of Elements of Inner List in List of Lists in Python
In addition to reversing the order of the inner lists, you can also reverse the order of the elements in the inner lists. For this, we will first create an empty list, say outputList
. After that, we will traverse through the list of lists in reverse order using a for a loop. Inside the for loop, we will create an empty list, say tempList
. After that, we will traverse through the elements of the inner list in reverse order using another for loop. While traversing the elements of the inner list, we will append the elements to the tempList
. Outside the inner loop, we will append the tempList
to outputList
.
After execution of the for loops, we will get a list with all the elements in reverse order as shown in the following code.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = []
listlen = len(myList)
for i in range(listlen):
tempList = []
currList = myList[listlen - 1 - i]
innerLen = len(currList)
for j in range(innerLen):
tempList.append(currList[innerLen - 1 - j])
outputList.append(tempList)
print("The reversed list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The reversed list is:
[[36, 24, 12], [33, 22, 11], [30, 20, 10], [23, 13, 12], [5, 4, 3, 2, 1]]
Instead of using the for loop to reverse the elements of the inner lists, you can use the reverse()
method as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = []
listlen = len(myList)
for i in range(listlen):
myList[listlen - 1 - i].reverse()
outputList.append(myList[listlen - 1 - i])
print("The reversed list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The reversed list is:
[[36, 24, 12], [33, 22, 11], [30, 20, 10], [23, 13, 12], [5, 4, 3, 2, 1]]
Here, we have first reversed the inner list inside the for loop. After that, we have appended it to outputList
. In this way, we have obtained the list of lists in which the inner lists, as well as the elements of the inner lists, are present in the reverse order as compared to the original list.
Sort List of Lists in Python
To sort a list of lists in python, we can use the sort() method or the sorted function.
Sort List of Lists in Python Using the sort() Method
The sort()
method, when invoked on a list, sorts the elements of the list in increasing order. When we invoke the sort()
method on a list of lists, it sorts the inner lists according to the first element of the inner lists.
In other words, the inner list whose first element is smallest among the first element of all the inner lists is assigned the first position in the list of lists. Similarly, the inner list whose first element is largest among the first element of all the inner lists is assigned the last position.
Also, if two inner lists have the same element at the first position, their position is decided on the basis of the second element. If the second element of the inner lists is also the same, the position of the lists will be decided on the basis of the third element, and so on. You can observe this in the following example.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
myList.sort()
print("The sorted list is:")
print(myList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The sorted list is:
[[1, 2, 3, 4, 5], [10, 20, 30], [11, 22, 33], [12, 13, 23], [12, 24, 36]]
You can also change the behavior of the sort()
method. For this, you can use the โkey
โ parameter of the sort()
method. The โkey
โ method takes an operator or a function as an input argument. For example, if you want to sort the list of lists according to the third element of the inner lists, you can pass an operator that uses the third element of the inner list as follows.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
myList.sort(key=lambda x: x[2])
print("The sorted list is:")
print(myList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The sorted list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
If you want to sort the list of lists according to the last elements of the inner lists, you can do it as follows.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
myList.sort(key=lambda x: x[-1])
print("The sorted list is:")
print(myList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The sorted list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
Similarly, if you want to sort the list of lists according to the length of the inner lists, you can pass the len()
function to the parameter โkey
โ of the sort()
method. After execution, the sort()
method will sort the list of lists using the length of the inner lists. You can observe this in the following example.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
myList.sort(key=len)
print("The sorted list is:")
print(myList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The sorted list is:
[[12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36], [1, 2, 3, 4, 5]]
Sorting List of Lists in Python Using the sorted() Function
If you are not allowed to modify the original list of lists, you can use the sorted()
function to sort a list of lists. The sorted()
function works in a similar manner to the sort()
method. However, instead of sorting the original list, it returns a sorted list.
To sort a list of lists, you can pass the list to the sorted()
function. After execution, the sorted()
function will return the sorted list as shown in the following example.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = sorted(myList)
print("The sorted list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The sorted list is:
[[1, 2, 3, 4, 5], [10, 20, 30], [11, 22, 33], [12, 13, 23], [12, 24, 36]]
You can also use the key
parameter to sort the list of lists using the sorted()
function. For instance, you can sort the list of lists according to the third element of the inner lists using the sorted()
function as shown below.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = sorted(myList, key=lambda x: x[2])
print("The sorted list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The sorted list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
If you want to sort the list of lists according to the last elements of the inner lists, you can do it as follows.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = sorted(myList, key=lambda x: x[-1])
print("The sorted list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The sorted list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
Similarly, if you want to sort the list of lists according to the length of the inner lists, you can pass the len()
function to the parameter โkey
โ of the sorted()
function. After execution, the sorted()
function will return the sorted list of lists using the length of the inner lists. You can observe this in the following example.
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = sorted(myList, key=len)
print("The sorted list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The sorted list is:
[[12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36], [1, 2, 3, 4, 5]]
Concatenate Two Lists of Lists in Python
If you are given two lists of lists and you want to concatenate the list of lists, you can do it using the + operator as shown below.
list1 = [[1, 2, 3, 4, 5], [12, 13, 23]]
list2 = [[10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The first list is:")
print(list1)
print("The second list is:")
print(list2)
print("The concatenated list is:")
myList = list1 + list2
print(myList)
Output:
The first list is:
[[1, 2, 3, 4, 5], [12, 13, 23]]
The second list is:
[[10, 20, 30], [11, 22, 33], [12, 24, 36]]
The concatenated list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
Here, the inner elements of both lists are concatenated into a single list of lists.
Copy List of Lists in Python
To copy a list of lists in python, we can use the copy()
and the deepcopy()
method provided in the copy
module.
Shallow Copy List of Lists in Python
The copy()
method takes a nested list as an input argument. After execution, it returns a list of lists similar to the original list. You can observe this in the following example.
import copy
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = copy.copy(myList)
print("The copied list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The copied list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The operation discussed in the above example is called shallow copy. Here, the inner elements in the copied list and the original list point to the same memory location. Thus, whenever we make a change in the copied list, it is reflected in the original list. Similarly, if we make a change in the original list, it is reflected in the copied list. To avoid this, you can use the deepcopy()
method.
Deep Copy List of Lists in Python
The deepcopy()
method takes a nested list as its input argument. After execution, it creates a copy of all the elements of the nested list at a different location and then returns the copied list. Thus, whenever we make a change in the copied list, it is not reflected in the original list. Similarly, if we make a change in the original list, it is not reflected in the copied list. You can observe this in the following example.
import copy
myList = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
print("The original list is:")
print(myList)
outputList = copy.deepcopy(myList)
print("The copied list is:")
print(outputList)
Output:
The original list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
The copied list is:
[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]
Conclusion
In this article, we have discussed the list of lists in python. We have discussed how we can perform various operations on a list of lists. We have also discussed how shallow copy and deep copy work with a list of lists. Additionally, we have discussed how to sort, reverse, flatten, and traverse a list of lists in python, To know more about the python programming language, you can read this article on dictionary comprehension in python. You might also like this article on file handling in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.