Ashti
Ashti

Reputation: 97

Removing certain sublists from a nested list based on a condition

So, I have a nested list (Let's call it A). It has a length of 2, and they should be considered separate lists. I want to iterate through these 2 separately, and remove the sublists that don't have equal length. For example, I want my output to be the same as A but with [['Table heading']] removed because it is not the same length as the other nested lists.

A=[[[['1'], ['2'], ['3'], ['4']],
  [['x'],['y'],['z'],['w']],
  [['a'],['b'],['c'],['d']],
  [['11'], ['22'], ['33'], ['44']]],
  [[['Table heading']],
  [['A'], ['B'], ['C'], ['D']],
  [['X'], ['Y'], ['Z'], ['W']],
  [['1'], ['2'], ['3'], ['4']]]]

 output=[[[['1'], ['2'], ['3'], ['4']],
  [['x'],['y'],['z'],['w']],
  [['a'],['b'],['c'],['d']],
  [['11'], ['22'], ['33'], ['44']]],
  [[['A'], ['B'], ['C'], ['D']],
  [['X'], ['Y'], ['Z'], ['W']],
  [['1'], ['2'], ['3'], ['4']]]]

Upvotes: 1

Views: 498

Answers (2)

Florian Bernard
Florian Bernard

Reputation: 2569

Like this

[ y for x in A for y in x if len(y) == 4]

Or in readable format.

out = []
for inner in A:
     for inner_inner in inner:
          if len(inner_inner) == 4:
               out.append(inner_inner)

If you don't know the size of inner list but, you know that there is only one list that do not match in size, you can do like this.

out = []
for inner in A:
     for inner_inner in inner:
         inner_size = len(inner_inner) 
         try:
             if inner_size == previous_size:
                 out.append(inner_inner)
          except NameError:
                  previous_size = inner_size
                  out.append(inner_inner)

This one have a default, if the first inner_inner element is to removed, the entire list elements will be removed instead of the first one.

Or like this

from collections import Counter

size = Counter([ len(y) for x in A for y in x ]).most_common(1)[0][0]
[ y for x in A for y in x if len(y) == size]

This solution loop two time over the entire list, depending on list sizes this could be a limitation.

Upvotes: 1

sleeping_coder
sleeping_coder

Reputation: 99

Check this out. It goes to delete the element unless they are not list anymore.


def  delete_odd(A):
    if isinstance(A[0],list)==False:
        return
    def delete_elem(a,if_not):
        print(a,'  ',if_not,'   ')

        i = 0
        while i<len(a):
            if len(a[i])!=if_not:
                del a[i]
            else:
                i+=1

    if len(A)<=2:
        pass
    else:
        n1 = len(A[0])
        n2 = len(A[1])
        n3 = len(A[2])
        if n1==n2:
            n = n1
        elif n2==n3:
            n = n2

        delete_elem(A,if_not=n)
    i = 0
    while i<len(A):
        delete_odd(A[i])
        i+=1




A = [[[['1'], ['2'], ['3'], ['4']],
  [['x'],['y'],['z'],['w']],
  [['a'],['b'],['c'],['d']],
  [['11'], ['22'], ['33'], ['44']]],
  [[['Table heading']],
  [['A'], ['B'], ['C'], ['D']],
  [['X'], ['Y'], ['Z'], ['W']],
  [['1'], ['2'], ['3'], ['4']]]]
delete_odd(A)

print(A)
#output
[[[['1'], ['2'], ['3'], ['4']], [['x'], ['y'], ['z'], ['w']], [['a'], ['b'], ['c'], ['d']], [['11'], ['22'], ['33'], ['44']]], [[['A'], ['B'], ['C'], ['D']], [['X'], ['Y'], ['Z'], ['W']], [['1'], ['2'], ['3'], ['4']]]]

Upvotes: 0

Related Questions