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()
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]]
Similar Reads
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Print lists in Python
Printing a list in Python is a common task when we need to visualize the items in the list. There are several methods to achieve this and each is suitable for different situations. In this article we explore these methods. The simplest way of printing a list is directly with the print() function: [G
3 min read
Python List methods
Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, weâll explore all Python list methods with a simple example. List MethodsLet's look at different list methods in Python: append(): Adds a
3 min read
llist module in Python
Up until a long time Python had no way of executing linked list data structure. It does support list but there were many problems encountered when using them as a concept of the linked list like list are rigid and are not connected by pointers hence take a defined memory space that may even be waste
3 min read
Python List Exercise
List OperationsAccess List ItemChange List itemReplace Values in a List in PythonAppend Items to a listInsert Items to a listExtend Items to a listRemove Item from a listClear entire listBasic List programsMaximum of two numbersWays to find length of listMinimum of two numbersTo interchange first an
3 min read
Read a CSV into list of lists in Python
In this article, we are going to see how to read CSV files into a list of lists in Python. Method 1: Using CSV moduleWe can read the CSV files into different data structures like a list, a list of tuples, or a list of dictionaries.We can use other modules like pandas which are mostly used in ML appl
2 min read
Python List extend() Method
In Python, extend() method is used to add items from one list to the end of another list. This method modifies the original list by appending all items from the given iterable. Using extend() method is easy and efficient way to merge two lists or add multiple elements at once. Letâs look at a simple
2 min read
How to Create a List of N-Lists in Python
In Python, we can have a list of many different kinds, including strings, numbers, and more. Python also allows us to create a nested list, often known as a two-dimensional list, which is a list within a list. Here we will cover different approaches to creating a list of n-lists in Python. The diffe
3 min read
Ways to Iterate Tuple List of Lists - Python
In this article we will explore different methods to iterate through a tuple list of lists in Python and flatten the list into a single list. Basically, a tuple list of lists refers to a list where each element is a tuple containing sublists and the goal is to access all elements in a way that combi
3 min read
Iterate over a list in Python
Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly. Example: Print all elements in the list one by one using for loop. [GFGTABS] Python a = [1, 3, 5, 7,
3 min read