Open In App

Python List of Lists

Last Updated : 29 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A list of lists in Python is a collection where each item is another list, allowing for multi-dimensional data storage. We access elements using two indices: one for the outer list and one for the inner list. In this article, we will explain the concept of Lists of Lists in Python, including various methods to create them and common operations that can be performed on Lists of Lists in Python.

Using List comprehension

List comprehension is an efficient way to create a list of lists in Python. It lets us generate nested lists in a single line, improving readability and performance.

Python
a = [[i for i in range(3)] for j in range(3)]
print(a)

Output
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

Using for loop

For loop with append() adds sublists one by one to a list. It's less efficient but offers flexibility for custom logic.

Python
a = []

# Loop through the range 0 to 2
for i in range(3):
  
    # Append a sublist [i, i+1, i+2] to 'a'
    a.append([i, i+1, i+2])
    
print(a)

Output
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

How to traverse a list of list ?

Let's understrand how to traverse a list of list.

Using for loop

Nested for loops are the easiest way to go through a List of Lists. It’s simple to understand and works well for most cases.

Python
a = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Traverse using nested for loops
for row in a :
    for element in row:
        print(element, end=" ")
    print()

Output
1 2 3 
4 5 6 
7 8 9 

How to delete an element from list of list ?

Let's understand how to delete an element from a list of list.

Using remove()

This delete the first occurrence of a specific value from a list. It modifies the list in place and raises an error if the value is not found.

Python
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

 # Removes the element 2 from the first sublist
a[0].remove(2) 
print(a)

Output
[[1, 3], [4, 5, 6], [7, 8, 9]]

How to Reverse List of Lists ?

Let's understand how to reverse a list of list.

Using reverse()

reverse() method reverses the list in place, modifying the original list without creating a new one. It doesn't use extra memory because it doesn't create a new list.

Python
a= [[1, 2], [3, 4], [5, 6]]
a.reverse()
print(a)

Output
[[5, 6], [3, 4], [1, 2]]

Using List slicing

This creates a new list with the elements in reverse order, leaving the original list unchanged. It's a quick and easy way to get a reversed copy of the list.

Python
a= [[1, 2], [3, 4], [5, 6]]
rev=a[::-1]
print(rev)

Output
[[5, 6], [3, 4], [1, 2]]

How to Sort List of Lists ?

Let's understand how to sort a list of list.

Using sorted()

sorted() function makes a new list arranged in order, based on a specific rule, like sorting by the first item in each sublist. We can choose how to sort the list using a custom rule.

Python
a= [[3, 4], [1, 2], [5, 6]]
res= sorted(a, key=lambda x: x[0])
print(res)

Output
[[1, 2], [3, 4], [5, 6]]

Using sort()

sort() method sorts the list in place, directly modifying the original list without creating a new one. It also allows using a custom key to determine the sorting order.

Python
a= [[3, 4], [1, 2], [5, 6]]
a.sort(key=lambda x: x[0])
print(a)

Output
[[1, 2], [3, 4], [5, 6]]

Next Article
Article Tags :
Practice Tags :

Similar Reads