0

I have something like this:

input: [['5', '-1'], ['3', '5'], ['6', '3'], ['7', '3']] and the code is '5'. Output should be: [['6', '3'], ['7', '3']]. So all elements that had a code were removed. I've tried doing something like this:

parent_ids =  [['5', '-1'], ['3', '5'], ['6', '3'], ['7', '3']]
print(parent_ids)

code = '5'

for parent_id in parent_ids:
    print(parent_id)

    for sublist in parent_id:
        if code in sublist:
            parent_ids.remove(parent_id)

But I get [['3', '5'], ['6', '3'], ['7', '3']] which is not 100% correct, it removed only 1 element

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56

1 Answers1

3

Try this :

parent_ids =  [['5', '-1'], ['3', '5'], ['6', '3'], ['7', '3']]
code = '5'
parent_ids = [i for i in parent_ids if not code in i]

Output :

[['6', '3'], ['7', '3']]
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56