1

I want to remove all nested list inside the nested list. Which means

x = [['-e'], ['-d', ['-e'], '-d'], ['-c', ['-d', ['-e'], '-d'], '-c'], ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']]

I want to remove the nested list in index 1,2,3,4... and make it a flat list. To make it clear the below is the separated values in the list.

['-e']
['-d', ['-e'], '-d']
['-c', ['-d', ['-e'], '-d'], '-c']
['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b']
['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']

I want it as

[['-e'], ['-d', '-e', '-d'], ['-c', '-d', '-e', '-d', '-c'], ['-b', '-c', '-d', '-e', '-d', '-c', '-b'], ['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']]

['-e']
['-d', '-e', '-d']
['-c', '-d', '-e', '-d', '-c']
['-b', '-c', '-d', '-e', '-d', '-c', '-b']
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']

And are there any way to get input like above.

for i in range(0,size):
        z = ['-{}'.format(alnum[size-i])]
        if alnum[size] != alnum[size-i]:
            x.append(z*2)
        else:
            x.append(z)

This was the snippet I used to get that x list.

3 Answers 3

1

This code uses recursion to achieve what you need.

from copy import deepcopy

x = [
    ['-e'],
    ['-d', ['-e'], '-d'],
    ['-c', ['-d', ['-e'], '-d'], '-c'],
    ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'],
    ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']
]

temp_list = []
def delist(x):
    for i in x:
        if type(i) == list:
            delist(i)
        else:
            temp_list.append(i)

new_list = []
for item in x:
    delist(item)
    new_list.append(deepcopy(temp_list))
    temp_list.clear()

print('Resultant:', new_list)
Sign up to request clarification or add additional context in comments.

Comments

0

With a helper recursive function for flattening nested sublists along with a list comprehension:

def nested_flatten(seq):
    # start with an empty list
    result = []
    
    # for each item in the sublist...
    for item in seq:
        # is item a list?
        if isinstance(item, list):
            # then extend the result with the flattened item
            result += nested_flatten(item)
        else:
            # otherwise extend with the item itself
            result += [item]
    return result

# invoke `nested_flatten` on each sublist
out = [nested_flatten(sub) for sub in x]

to get

>>> out

[['-e'],
 ['-d', '-e', '-d'],
 ['-c', '-d', '-e', '-d', '-c'],
 ['-b', '-c', '-d', '-e', '-d', '-c', '-b'],
 ['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']]

3 Comments

I am a begginer so can you explain this.'result += nested_flatten(item)'
@GoAmeer030 The function starts with an empty list. Then checks each item in the sublist, e.g., ['-c', ['-d', ['-e'], '-d'], '-c']. If the item is not a list (for example first one), then it puts it into the result list as is. But if it is a list (for example second one ['-d', ['-e'], '-d']), then it calls itself! and this procedure goes on. Each call at the end returns a list. The list comprehension at the end calls this function for each element of x.
@GoAmeer030 If you're not super familiar with recursive functions, then I might have confused you even more... Sorry if that's the case but otherwise I hope it helped...
0

python-convert-list-of-lists-or-nested-list-to-flat-list

def flattenNestedList(nestedList):
    ''' Converts a nested list to a flat list '''
    flatList = []
    # Iterate over all the elements in given list
    for elem in nestedList:
        # Check if type of element is list
        if isinstance(elem, list):
            # Extend the flat list by adding contents of this element (list)
            flatList.extend(flattenNestedList(elem))
        else:
            # Append the elemengt to the list
            flatList.append(elem)    
    return flatList

x = [['-e'], ['-d', ['-e'], '-d'], ['-c', ['-d', ['-e'], '-d'], '-c'], ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']]

for li in x:
    print(flattenNestedList(li))

output:

['-e']
['-d', '-e', '-d']
['-c', '-d', '-e', '-d', '-c']
['-b', '-c', '-d', '-e', '-d', '-c', '-b']
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.