This is a Python Program to flatten a nested list without using recursion.
The program takes a nested list and flattens the nested list without using recursion.
1. A variable must be initialized with a nested list.
2. Use a lambda function and map() to map the flatten function to each element in the nested list to flatten the list.
3. Print the flattened list.
4. Exit.
Here is source code of the Python Program to flatten a nested list without using recursion. The program output is also shown below.
a=[[1,[[2]],[[[3]]]],[[4],5]] flatten=lambda l: sum(map(flatten,l),[]) if isinstance(l,list) else [l] print(flatten(a))
1. A variable is initialized with a nested list.
2. Using a lambda function and map(), the function flatten is mapped to each element in the nested list to flatten the list.
3. The flattened list is then printed.
Case 1: [1, 2, 3, 4, 5]
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Apply for Python Internship
- Check Information Technology Books
- Practice Programming MCQs
- Check Python Books
- Apply for Programming Internship