90. Problem to flatten given nested list structure.

In this Python list program, we will take a user input as a nested list and flatten given nested list structure with the help of the below-given steps.

Flatten given nested list:

Steps to solve the program
  1. Take a nested list as input.
  2. Flatten given nested into a single list using for loop.
  3. If the iterated element is a list then add the elements in that list to the empty list.
  4. Use the type() function to check for list type.
  5. If not then also add the element to the empty list.
  6. Print the list to see the result.
				
					#Input lists
list1 = [0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122]]
list2 = []

for value in list:
    if type(value) is list:
        for var in value:
            list2.append(var)
    else:
        list2.append(value)

#Printing output
print(list2)
				
			

Output :

				
					[0, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122]
				
			

Related Articles

Get the list of palindrome strings

Convert tuples in list into a sublist

Leave a Comment