0

How can I solve the following question?

Given a list which contains sub-lists also.
For ex:-

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

I want to split the list into a single list while removing all the sub-lists.

The output should be

[1,2,3,4,5,6,7,8,9,10]
cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    This is called "flattening nested lists". If you search for it you'll find many solutions. – Barmar Jul 10 '21 at 08:40
  • Ok, Thanks let me check it, first If I'll catch any doubt I'll ask you. – Kapil Yadav Jul 10 '21 at 08:41
  • See https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists – Barmar Jul 10 '21 at 08:44
  • Does this answer your question? [Flatten an irregular list of lists](https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists) – cigien Jul 10 '21 at 12:22

4 Answers4

1

Basically, you iterate over the list and see if the current element is a list itself. If it's not, add it to a new list. If it is, iterate over the list and repeat.

It's not the nicest, smallest or most python-way but this way might help you to understand it:

list_in = [1,2,[3,4],5,[6,7,[8,9,10]]]
new_list = []

def unfold_list(l):
    for x in l:
        if isinstance(x, list):
            unfold_list(x)
        else:
            new_list.append(x)

unfold_list(list_in)
print(new_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note regarding Barmar's comment on Sujay's answer: Using globals should indeed be avoided. My code above is purely meant to help understand the issue and (one of the) solution(s).

iScripters
  • 403
  • 3
  • 13
1

I think the problem is well-suited for recursion:

def flatten(x, r=[]):
    if type(x) is list:
        temp = []
        for i in x:
            temp += flatten(i)
        return r+temp
    else:
        return [x]

flatten([1, 2, [3, 4], 5, [6, 7, [8, 9, 10]]])
#output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
  • Hey thanks I got the logic. But my question is can I do it without defining any function? – Kapil Yadav Jul 10 '21 at 08:53
  • If you don't mind using lambda, there's a way to do it posted by samplebias at https://stackoverflow.com/a/5409395/2111837 – iScripters Jul 10 '21 at 08:55
  • You can do without defining a function @KapilYadav only if the list is always structured the way it is present in sample data. – ThePyGuy Jul 10 '21 at 09:01
0

Check this out:

new_l=[]
def flatten(lists):
    for i in lists:
        if isinstance(i,list):
            flatten(i)
        else:
            new_l.append(i)
    return new_l
list2d = [1,2,[3,4],5,[6,7,[8,9,10]]]
print(flatten(list2d))
  • Yuo shouldn't use a global variable. If you run it twice, you'll concatenate the two results. – Barmar Jul 10 '21 at 08:43
0

Try this

def flatten(list_of_lists):
    if len(list_of_lists) == 0:
        return list_of_lists
    if isinstance(list_of_lists[0], list):
        return flatten(list_of_lists[0]) + flatten(list_of_lists[1:])
    return list_of_lists[:1] + flatten(list_of_lists[1:])

print(flatten([1, 2, [3, 4], 5, [6, 7], [8, 9, 10]]))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t4kq
  • 754
  • 1
  • 5
  • 11