Open In App

Iterate Over a List of Lists in Python

Iterating over a list of lists is a common task in Python, especially when dealing with datasets or matrices. In this article, we will explore various methods and techniques for efficiently iterating over nested lists, covering both basic and advanced Python concepts. In this article, we will see how we can iterate over a list of lists in Python.

Iterate Over a Nested List in Python

Below are some of the ways by which we can iterate over a list of lists in Python:



Iterating Over a List of Lists

In this example, a list named `list_of_lists` is created, containing nested lists. Using nested for loops, each element in the inner lists is iterated over, and the `print` statement displays the elements horizontally within each sublist, with each sublist on a new line.




list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  
for sublist in list_of_lists:
    for item in sublist:
        print(item, end=' ')
    print()

Output

1 2 3 
4 5 6 
7 8 9 


Using List Comprehension

In this example, a nested list named `nested_list` is created. List comprehension is used to flatten the nested structure into a single list named `flattened_list`. The resulting flattened list is then printed, showcasing a concise and powerful approach to list manipulation.




nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  
flattened_list = [item for sublist in nested_list for item in sublist]
  
print(flattened_list)

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


Enumerating Over a Nested List

In this example, a list named `languages` is created, representing programming languages. The enumerate() function is utilized in a for loop to iterate over the list, providing both the index and the language during each iteration. The `print` statement displays the indexed list of programming languages with enumeration starting from 1.




nested_list = [[1, 2, 3], [4, 5], [7, 8]]
  
for i, inner_list in enumerate(nested_list):
    for j, element in enumerate(inner_list):
        print(f"Value at index ({i}, {j}): {element}")

Output
Value at index (0, 0): 1
Value at index (0, 1): 2
Value at index (0, 2): 3
Value at index (1, 0): 4
Value at index (1, 1): 5
Value at index (2, 0): 7
Value at index (2, 1): 8


Using itertools.chain() Function

In this example, the itertools.chain() function is employed to flatten a nested list named `nested_list`. The `*nested_list` syntax is used to unpack the inner lists, and the result is a flattened list, which is then printed.




from itertools import chain
  
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  
flattened_list = list(chain(*nested_list))
print(flattened_list)

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


Conclusion

So, Overall, understanding the syntax and various operations associated with Python lists is essential for efficient data manipulation and iteration. Whether you are working with a simple list or a list of lists, Python’s list capabilities provide a powerful foundation for data handling.


Article Tags :