user16174931
user16174931

Reputation:

How to Remove nested list which is inside a nested list in python?

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.

Upvotes: 1

Views: 1589

Answers (3)

Mohsin Ali
Mohsin Ali

Reputation: 11

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)

Upvotes: 1

ShiraStar
ShiraStar

Reputation: 126

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']

Upvotes: 0

Mustafa Aydın
Mustafa Aydın

Reputation: 18315

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']]

Upvotes: 0

Related Questions