Unfold a list within a list in Python

Question

Input = [1,2,3,5,[45,56,67],4]

Output = [1,2,3,5,45,56,67,4]

in progress 0
TheDataMonk 55 years 3 Answers 704 views Grand Master 0

Answers ( 3 )

  1. Output=[]
    for i in Input:
    if type(Input) is ‘list’:
    for value in i:
    Output.append(value)
    else:
    Output.append(i)
    Output

  2. Output=[]
    def checkType(a_list):
    for element in a_list:
    if isinstance(element, int):
    Output.append(element)
    if isinstance(element, list):
    for i in element:
    Output.append(i)
    return Output

    Input = [1,2,3,5,[45,56,67],4]
    print(checkType(Input))

  3. new_list = []
    for i in range(len(original_list)):
    if type(original_list[i]) != list:
    updated.append(original_list[i])
    else:
    for j in range(0,len(original_list[i])):
    updated.append(original_list[i][j])

    print(new_list)

Leave an answer

Browse
Browse