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

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.

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 the append() Method in Python
    2. Create List of Lists Using List Comprehension in Python
  3. Access Elements in a List of Lists in Python
  4. Traverse a List of Lists in Python
  5. Delete an Element From a List of Lists in Python
    1. Delete an Element From a List of Lists Using the pop() Method
    2. Remove an Element 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 the Order of Inner List in List of Lists in Python
    2. Reversing the Order of Elements of Inner List in List of Lists in Python
  8. Sort List of Lists in Python
    1. Sort List of Lists in Python Using the sort() Method
    2. Sorting List of Lists in Python Using the sorted() Function
  9. Concatenate Two Lists of Lists in Python
  10. Copy List of Lists in Python
    1. Shallow Copy List of Lists in Python
    2. Deep Copy List of Lists in Python
  11. 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.

(Video) Python Programming 42 - Working with List of Lists (2D Lists)

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:

(Video) How to process List of lists in PYTHON!

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.

(Video) How to Use Lists in Python

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:

(Video) 9 - Looping over a list of lists in Python, Presented by Dr N. Miri

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 + list2print(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 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)

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 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)

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.

Related

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.

Enroll Now

(Video) Python Tutorial for Beginners 4: Lists, Tuples, and Sets

FAQs

How do I get a list of lists in Python? ›

What is a list of lists? In any programming language, lists are used to store more than one item in a single variable. In Python, we can create a list by surrounding all the elements with square brackets [] and each element separated by commas.

What does list [:] Do Python? ›

The list() function creates a list object. A list object is a collection which is ordered and changeable. Read more about list in the chapter: Python Lists.

How do I find out how many lists are in a list Python? ›

Len() Method

There is a built-in function called len() for getting the total number of items in a list, tuple, arrays, dictionary, etc.

What are 3 types of list in Python? ›

Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

Can you have an array of lists in Python? ›

Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.

Why do people use lists? ›

By keeping such a list, you make sure that your tasks are written down all in one place so you don't forget anything important. And by prioritizing tasks, you plan the order in which you'll do them, so that you can tell what needs your immediate attention, and what you can leave until later.

How do you create a list in Python? ›

To create a list in Python, we use square brackets ( [] ). Here's what a list looks like: ListName = [ListItem, ListItem1, ListItem2, ListItem3, ...] Note that lists can have/store different data types.

What is the purpose of using lists? ›

Helps you to arrange things in order.

Simply putting a number in front of each list item helps you to arrange your list into an order that is meaningful. Once you have something in list order, numbering becomes easier.

How do I see multiple lists in Python? ›

Iterate over multiple lists at a time

We can iterate over lists simultaneously in ways: zip() : In Python 3, zip returns an iterator. zip() function stops when anyone of the list of all the lists gets exhausted. In simple words, it runs till the smallest of all the lists.

How do I get a list of Sublists? ›

To get the subarray we can use slicing to get the subarray. Step 1: Run a loop till length+1 of the given list. Step 2: Run another loop from 0 to i. Step 3: Slice the subarray from j to i.

What is list () constructor in Python? ›

The list() constructor returns a list. If no parameters are passed, it returns an empty list. If iterable is passed as a parameter, it creates a list consisting of iterable's items.

What are the two 2 types of lists? ›

unordered list — used to group a set of related items in no particular order. ordered list — used to group a set of related items in a specific order.

What are the basic types of lists? ›

An example of the three list types, which are unordered list, ordered list, and definition list.

How to combine 2 lists into 1 in Python? ›

Python's extend() method can be used to concatenate two lists in Python. The extend() function does iterate over the passed parameter and adds the item to the list thus, extending the list in a linear fashion. All the elements of the list2 get appended to list1 and thus the list1 gets updated and results as output.

How do you combine all elements in a list Python? ›

Using join() method to concatenate items in a list to a single string. The join() is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string.

How do you concatenate multiple lists in Python? ›

Using Python itertools.

chain() method to concatenate multiple lists together. The itertools. chain() method accepts data of different iterables such as lists, string, tuples, etc and provides a linear sequence of elements out of them. This function works irrespective of the data type of the input data.

Is it better to use arrays or lists in Python? ›

An array is faster than a list in python since all the elements stored in an array are homogeneous i.e., they have the same data type whereas a list contains heterogeneous elements. Moreover, Python arrays are implemented in C which makes it a lot faster than lists that are built-in in Python itself.

Are Python lists arrays or lists? ›

First of all, lists are part of the core Python programming language; arrays are a part of the numerical computing package NumPy. Therefore, you have access to NumPy arrays only if you load the NumPy package using the import command.

Why you should stop making to do lists? ›

A To-Do List Creates A False Sense of Productivity. In reality, it's precisely these high-impact tasks that we tend to procrastinate on the most. They are often more complex, time-consuming, and energy-draining.

Why is it important not to do lists? ›

To-do lists can work for you, but if you are not using them effectively, they can actually leave you feeling more disillusioned and stressed than you did before. Think of a filing system: the concept is good, but if you merely file papers away with no structure or system, the filing system will have an adverse effect.

How do you use lists correctly? ›

Punctuation for Lists of Items Within a Sentence

Use commas after each item in a list of three or more items. Nurses monitor a patient's vital signs including temperature, blood pressure, respiratory rate, and pulse. Use semi-colons after each item in a list if one or more items already includes a comma.

How do you access sublist elements in Python? ›

Using zip and *

The * allows us to unpack the sublists and give access to individual elements of the sublist. So in this case we will use * and access the element at index 0 from each element. Then we finally zip the result to get a list of the first element from the sublists.

How do you combine lists in Python? ›

One simple and popular way to merge(join) two lists in Python is using the in-built append() method of python. The append() method in python adds a single item to the existing list. It doesn't return a new list of items. Instead, it modifies the original list by adding the item to the end of the list.

Videos

1. Python group or sort list of lists by common element
(Softhints - Python, Linux, Pandas)
2. Python Lists & Accessing from List with examples | Python for Beginners
(Gate Smashers)
3. Python for Beginners — Lists
(CODE MENTAL)
4. #5 Python Tutorial for Beginners | List in Python
(Telusko)
5. List With in The List in Python || Lesson 21 || Python || Learning Monkey ||
(Learning Monkey)
6. 30 Days of Python - Day 3 - Lists & Dictionaries - Python TUTORIAL
(CodingEntrepreneurs)
Top Articles
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated: 12/03/2022

Views: 6606

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.