Flatten a Nested List in Python Using Lambda and Reduce()

(Last Updated On: 23/01/2023)

Python Program to Flatten a Nested List in Python Using Lambda and Reduce()

#Flatten a Nested List in Python Using Lambda and Reduce() Function

from functools import reduce

my_list = [[1], [2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]

# Output reduce() applies the lambda function to all the elements of my_list
print(reduce(lambda x, y: x+y, my_list))

Output:

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

Leave a Reply

Your email address will not be published. Required fields are marked *