Indexing Lists Of Lists In Python
Last Updated :
12 Feb, 2024
Lists of lists are a common data structure in Python, providing a versatile way to organize and manipulate data. When working with nested lists, it’s crucial to understand how to index and access elements efficiently. In this article, we will explore three methods to index lists of lists in Python using the creation of a sample list, followed by examples using slicing, for loops, and list comprehensions.
Example
Input: [[1, 2, 3], [4,5,6],[7,8,9]]
Output: 6
Indexing Lists Of Lists In Python
Below, are the methods of Indexing Lists Of Lists In Python.
Creating a List of Lists
Before delving into indexing methods, let’s start by creating a sample list of lists: For the purpose of this article, we’ll use this matrix
as our sample list of lists.
Python3
matrix = [
[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]
]
|
Indexing Lists Of Lists In Python Using For Loop
In this example, The code demonstrates indexing a list of lists using a nested for loop. It iterates through each row and column of the matrix, printing the element at each position along with its coordinates.
Python3
rows = len (matrix)
columns = len (matrix[ 0 ])
print ( "\nUsing For Loop:" )
for i in range (rows):
for j in range (columns):
print (f "Element at ({i}, {j}): {matrix[i][j]}" )
|
Output
Using For Loop:
Element at (0, 0): 1
Element at (0, 1): 2
Element at (0, 2): 3
Element at (1, 0): 4
Element at (1, 1): 5
Element at (1, 2): 6
Element at (2, 0): 7
Element at (2, 1): 8
Element at (2, 2): 9
Indexing Lists Of Lists In Python Using List Comprehension
In this example, below code utilizes list comprehension to flatten a list of lists (matrix
) into a single list (flat_list
). It succinctly combines elements from each row into a unified structure, resulting in a flattened representation of the original nested data.
Python3
flat_list = [element for row in matrix for element in row]
print ( "\nUsing List Comprehension:" )
print ( "Flattened List:" , flat_list)
|
Output
Using List Comprehension:
Flattened List: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Indexing Lists Of Lists In Python Using Slicing
In this example, This code showcases indexing a list of lists using slicing. It extracts the first row and the second column from the matrix, demonstrating the application of slicing to access specific sections of nested data efficiently.
Python3
first_row = matrix[ 0 ]
second_column = [row[ 1 ] for row in matrix]
print ( "Using Slicing:" )
print ( "First Row:" , first_row)
print ( "Second Column:" , second_column)
|
Output
Using Slicing:
First Row: [1, 2, 3]
Second Column: [2, 5, 8]
Conclusion
Indexing lists of lists in Python is a fundamental skill, and the methods discussed in this article – using slicing, for loops, and list comprehensions – provide different approaches to access and manipulate nested data structures efficiently. Choose the method that best fits your specific use case for optimal code readability and performance.
Please Login to comment...