The task is to convert a nested list i.e [1,2,4,7,[4,5,7,[5,1,7,8,],8,5,],7,8,9] into a single list i.e
#NOTE: whole inner list inside nested list treat as single element
[1, 2, 4, 7, 4, 5, 7, 5, 1, 7, 8, 8, 5, 7, 8, 9] in python,no matter how many levels of
nesting is there in python list, all the nested has to be removed in order to convert it to a single containing all the values of all the lists inside the outermost brackets but without any brackets inside.
#NOTE: whole inner list inside nested list treat as single element
#initializing variables
nested_list=[1,2,4,7,[4,5,7,[5,1,7,8,],8,5,],7,8,9]
final_list=[]
#function used to convert
def remove_nested(nested_list):
for data in nested_list: #loop to goes each element
if type(data)==list: #check if the single element in nested_list is single of list
remove_nested(data) #run recursive for list present into nested_list
else:
final_list.append(data) # add to empty list if single element is to list type
remove_nested(nested_list) #call function
print(final_list)
--------Thank You Coders-------
-------drop your questions in comment box-------