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:Pyth
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 an
3 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
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.Pythona = [1, 3, 5, 7, 9] # On each
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
Internal working of list in Python Introduction to Python lists :Â Python lists are internally represented as arrays. The idea used is similar to implementation of vectors in C++ or ArrayList in Java. The costly operations are inserting and deleting items near the beginning (as everything has to be moved). Insert at the end also becom
3 min read
Update List in Python In Python Programming, a list is a sequence of a data structure that is mutable. This means that the elements of a list can be modified to add, delete, or update the values. In this article we will explore various ways to update the list. Let us see a simple example of updating a list in Python.Pyth
2 min read
Cloning or Copying a List - Python In Python, lists are mutable, meaning they can be modified after creation. Often, we may need to create a copy of a list to preserve the original data while making changes to the duplicate. Cloning or copying a list can be done in multiple ways, each with its own characteristics. Let's discuss vario
3 min read
Python - Convert list of string to list of list In Python, we often encounter scenarios where we might have a list of strings where each string represents a series of comma-separated values, and we want to break these strings into smaller, more manageable lists. In this article, we will explore multiple methods to achieve this. Using List Compreh
3 min read