List of Lists in Python - PythonForBeginners.com (2023)

Lists are used in Python to store data when we need to access them one by one. In this article, we'll discuss how we can create a list of lists in Python. We'll also implement programs to perform various operations such as sorting, iterating, and reversing a list of lists in Python.

table of contents

  1. What is a list of lists in Python?
  2. Create a List of Lists in Python
    1. List of lists using append() method in Python
    2. Create a list of lists using Python list comprehension
  3. Accessing Elements in a List of Lists in Python
  4. Iterate over a list of lists in Python
  5. Remove an item from a list of lists in Python
    1. Remove an item from a list of lists using the pop() method
    2. Remove an item from a list of lists using the remove() method
  6. Flatten List of Lists in Python
  7. Reverse list of lists in Python
    1. Reverse inner list order in list of lists in Python
    2. Reverse order of inner list items in list of lists in Python
  8. Sort list of lists in Python
    1. Sort a list of lists in Python using the sort() method
    2. Sorting lists in Python with the sorted() function
  9. Concatenate two lists of lists in Python
  10. Copy list of lists in Python
    1. List shallow copy of lists in Python
    2. Deep copy list of lists in Python
  11. Conclusion

What is a list of lists in Python?

a list ofListen in Pythonis a list containing lists as elements. The following is an example of a list of lists.

meineListe=[[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]

On here,my listcontains five lists as its elements. So it's a list of lists.

Create a List of Lists in Python

To create a list of lists in Python, you can use square brackets to store all the internal lists. For example, if you have 5 lists and you want to create a list of lists from the given lists, you can enclose 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)

Production:

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 example above, you can see that we've created a list of lists from lists.

List of lists using append() method in Python

We can also create a list of lists using theattach ()method in Python. Theattach ()The method, when called on a list, takes an object as input and adds it to the end of the list. To create a list of lists, use theattach ()method, we first create a new empty list. You can use bracket notation or theList()Constructor. TheList()The constructor returns an empty list when executed with no input arguments.

After creating the empty list, we can add any specified list to the created list usingattach ()- Method to create a list of lists in Python according to the code snippet below.

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)

Production:

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 as a 2-D array using only integer data types, you can use nested for loops withattach ()Method for creating a list of lists.

For example, in this approach, we first create a new listmy list. After that we use a nested for loop to add other listsmy list. For example, in the outer for loop of the nested loop, we create another empty listtime list. In the inner for loop, we add the numbers totime listUse ofattach ()Method.

After adding the numberstime list, we get a list of integers. After that we get to the outer for loop and addtime listanmy list. In this way we can create a list of lists.

Suppose we need to create a 3 × 3 matrix of numbers. For this we use theReach()function and thein loopto 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("A lista de listas é: ")imprimir(curtir)

Production:

The list of lists is: [[0, 1, 2], [1, 2, 3], [2, 3, 4]]

Create a list of lists using Python list comprehension

Instead of using the for loop, you can usecomprehension listwith himReach()Function to create an ordered list of lists as shown in the following example.

miList = [[i+j for i in range(3)] for j in range(3)]print("The list of lists is:") print(myList)

Production:

The list of lists is: [[0, 1, 2], [1, 2, 3], [2, 3, 4]]

Accessing Elements in a List of Lists in Python

We can access the contents of a list through the index of the list. In a flat list or one-dimensional list, we can access the elements of the list directly using the index of the elements. For example, if we want to use positive values ​​as an index for the list items, we can access the first list item with index 0 as shown below.

myList = [1, 2, 3, 4, 5]print("The list is:") print(myList)print("The first element of the list is:") print(myList[0])

Production:

The list is:[1, 2, 3, 4, 5]The first element of the list is:1

Likewise, if we use the negative values ​​as indexes of the list, we can access the last element of the list using index -1 as shown below.

myList = [1, 2, 3, 4, 5]print("The list is:") print(myList)print("The last item in the list is:") print(myList[-1])

Production:

The list is:[1, 2, 3, 4, 5]The last element of the list is:5

If you want to access lists internal to a list of lists, you can use list indexes similar to the previous example.

If you use positive numbers as list indices, you can access the first inner list of 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 element of the nested list is:")print(myList[0])

Production:

The list of lists is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36] ]The first element of the nested list is:[1, 2, 3, 4, 5]

Likewise, using negative numbers as list indices, you can access the last inner list of 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 element of the nested list is:")print(myList[-1])

Production:

The list of lists is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36] ]The last element of the nested list is:[12, 24, 36]

To access elements of built-in lists, you must use double square brackets after the list name. The first brackets designate the index of the inner list and the second brackets the index of the element in the inner list.

For example, you can access the third item in the second list of 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])

Production:

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

Iterate over a list of lists in Python

To iterate through the elements of a list of lists, we can use a for loop. To print the internal lists, we can simply loop 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(internal_list)

Production:

[1, 2, 3, 4, 5][12, 13, 23][10, 20, 30][11, 22, 33][12, 24, 36]

Instead of printing the entire list while iterating over the list of lists, we can also print the elements of the list. To do this, we use another for loop in addition to the for loop shown in the previous example. In the inner for loop, we iterate over 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("")

Production:

1,2,3,4,5,12,13,23,10,20,30,11,22,33,12,24,36,

Remove an item from a list of lists in Python

To remove an inner list from a list of lists, we can use different methods of list objects.

Remove an item from a list of lists using the pop() method

we can use thosePop()Method to remove the last item from the list of lists. ThePop()The method, when called on the list of lists, removes the last element and returns the existing list in the last position. We can understand this with 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)

Production:

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 remove another internal list, we need to know its index. For example, we can remove the second item from the list of lists withPop()Method. For this we refer to thePop()in the list and passes the index of the second list, i.e. 1 to thePop()Method. After execution, thePop()The method removes the second inner list from the list of lists and returns 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)

Production:

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 item from a list of lists using the remove() method

If we know the element to remove, we can also use theextinguish()Method for deleting an internal list. Theextinguish()The method, when called on a list, takes the element to be removed as its input argument. After execution, the first occurrence of the element passed as an input argument is removed. To delete an internal list, we can use theextinguish()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)

Production:

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 one-dimensional list. To smooth the list of lists, we can use a for loop and theattach ()Method. In this approach, we first create, say, an empty listinitial list.

after creatinginitial listwe use a nested for loop to loop through the list of lists. In the outer loop, we select an inner list. After that, we iterate over the elements of the inner list in the inner for loop. In the inner loop, we callattach ()method andinitial listand passes the inner for loop elements as input arguments to theattach ()Method.

After executing the for loops, we get a flat list created from the list of lists, as shown in the following code.

minhaLista = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]print(" A lista original é:")print(myList)outputList = []for inner_list in myList: for element in inner_list: outputList.append(element)print("A lista aplanada é:")print(outputList)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The simplified 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 simplified list is:")print(outputList)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The simplified 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 canreverse a listof lists in two ways. One approach is to simply reverse the order of the inner lists and leave the order of elements in the inner lists intact. Another approach is also to reverse the order of items in the inner lists.

Reverse inner list order in list of lists in Python

For example, to make it easier to reverse the order of inner lists, first create an empty listinitial list. After that, we iterate through the list of lists in reverse order. During the tour we will add the internal listsinitial list. In this way we get the inverted list of listsinitial listafter running the for loop. You can see this in the example 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): outputList.append(myList[listlen - 1 - i])print("The inverted list is: ")print(output list)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The inverted 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 thego back()Method to reverse the list of lists. Thego back()The method reverses the order of elements in the list when called on a list. when we call themgo back()in the list of lists, the order of the inner lists is reversed, 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)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The inverted 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, this was not the case in the previous example. So you can take one approach depending on whether or not you need to change the original list.

Reverse order of inner list items in list of lists in Python

In addition to reversing the order of built-in lists, you can also reverse the order of items in built-in lists. To do this, we first create B. an empty listinitial list. After that, we iterate through the list of lists in reverse order using a for loop. For example, inside the for loop, we create an empty listtime list. After that, we iterate over the inner list elements in reverse order with another for loop. As we iterate through the elements of the inner list, we will add elements to the list.time list. Outside the inner loop, we add thetime listaninitial list.

After executing the for loops, we get a list with all the elements in reverse order, as shown in the code below.

minhaLista = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]print(" A lista original é:") print(myList)outputList = []listlen = len(myList)for i in range(listlen): tempList = [] currList = myList[listlen - 1 - i] innerLen = len(currList) for j no intervalo (comprimento interno): templist.append(currentlist[internallength - 1 - j]) outputlist.append(templist) print("A lista invertida é:") print(outlist)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The inverted 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 for loopgo back()method as shown below.

miLista = [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]]imprimir(" A lista original é:")print(myList)outputList = []listlen = len(myList)for i in range(listlen): myList[listlen - 1 - i].reverse() outputList.append(myList[listlen - 1 - i])print("A lista invertida é:")print(outputList)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The inverted list is: [[36, 24, 12], [33, 22, 11], [30, 20, 10], [23, 13, 12], [5, 4, 3, 2, 1]]

Here, we first reverse the inner list inside the for loop. After that, we attachinitial list. Thus, we got the list of lists where both the inner lists and the elements of the inner lists are in reverse order 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 a list of lists in Python using the sort() method

Diearrange by()The method, when called on a list, sorts the elements of the list in ascending order. when we call themarrange by()in a list of lists, sorts the inner lists by the first element of the inner lists.

In other words, the inner list whose first element is the smallest among the first elements of all inner lists is given the first position in the list of lists. Likewise, the inner list whose first element is the largest among the first elements of all inner lists is given the last position.

Even if two inner lists have the same element in the first position, its position is decided based on the second element. If the second element of the inner lists is also the same, the third element is used to determine the position of the lists, and so on. You can see this in the example 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.sort()print("The sorted list is:")print(myList)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The ordered 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 ofarrange by()Method. To do this, you can use the 'spannerparameters ofarrange by()Method. Him 'spannerThe method accepts an operator or a function as an input argument. For example, if you want to sort the list of lists by the third element of the inner list, 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)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The ordered 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 by the last elements of the inner lists, you can do it in the following way.

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)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The ordered 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 by the length of the inner lists, you can pass thelargo()Function for parameter 'spanner' from toarrange by()Method. After execution, thearrange by()The method sorts the list of lists based on the length of the inner lists. You can see this in the example 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.sort(key=len)print("The sorted list is:")print(myList)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The ordered list is: [[12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36], [1, 2, 3, 4, 5]]

Sorting lists in Python with the sorted() function

If you are not allowed to change the original list of lists, you can use theclean ()Function to sort a list of lists. Theclean ()The function works in a similar way.arrange by()Method. However, instead of sorting the original list, it returns a sorted list.

To sort a list of lists, you can pass the list toclean ()Occupation. After execution, theclean ()The function returns 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)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The ordered list is: [[1, 2, 3, 4, 5], [10, 20, 30], [11, 22, 33], [12, 13, 23], [12, 24, 36]]

You can also use thespannerParameters to sort the list of lists with theclean ()Occupation. For example you can use to sort the list of lists by the third element of the inner listsclean ()work 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)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The ordered 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 by the last elements of the inner lists, you can do it in the following way.

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)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The ordered 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 by the length of the inner lists, you can pass thelargo()Function for parameter 'spanner' from toclean ()Occupation. After execution, theclean ()The function returns the sorted list of lists using the length of the inner lists. You can see this in the example 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=len)print("The sorted list is:")print(outputList)

Production:

The original list is: [[1, 2, 3, 4, 5], [12, 13, 23], [10, 20, 30], [11, 22, 33], [12, 24, 36]] The ordered 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 get two lists of lists and you want to concatenate the list of lists, you can do so with 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 linked list is:") myList = list1 + list2print(myList)

Production:

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 linked 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 theCopy()it's himdeep copy()method provided inCopyModule.

List shallow copy of lists in Python

DieCopy()The method accepts a nested list as an input argument. Upon execution, returns a list of lists similar to the original list. You can see this in the example below.

import copymyList = [[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)

Production:

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 example above is called shallow copying. The inner elements of the copied list and the original list point to the same memory location. Therefore, if we make a change to the copied list, it will be reflected in the original list. Likewise, if we make a change to the original list, it will be reflected in the copied list. To avoid this, you can use thedeep copy()Method.

Deep copy list of lists in Python

Diedeep copy()The method takes a nested list as an input argument. Upon execution, it creates a copy of all elements of the nested list elsewhere and returns the copied list. Therefore, if we make a change to the copied list, it will not be reflected in the original list. Likewise, if we make a change to the original list, it will not be reflected in the copied list. You can see this in the example below.

import copymyList = [[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)

Production:

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 discuss list of lists in Python. We discussed how we can perform various operations on a list of lists. We also discussed how shallow copy and deep copy work with a list of lists. In addition, we discussed how to sort, invert, flatten, and iterate through a list of lists in Python. To learn more about the Python programming language, you can continue reading this article.Understanding Dictionaries in Python. You may also like this articlePython file management.

Related

Recommended Python training

Course: Python 3 for Beginners

15+ hours of video content with guided instruction for beginners. Learn to build real apps and master the basics.

register now

Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated: 12/01/2022

Views: 5927

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.