Skip to content Skip to sidebar Skip to footer

Deleting Elements From A List If They Do Not Follow 'if' 'or' Statements

I am trying to get rid of unwanted variables in a list. I need to have two condition: one is if making sure the values in my array are smaller than a variable A, and the other is m

Solution 1:

You have your boolean logic mixed up. You want to include all values that are not equal to 2 and are smaller than 10:

new_Ar = [s for s in original_Ar if s != 2 and s < 10]
#           *both* conditions must be true ^^^

Otherwise, you'd include s = 2, because it is smaller than ten, and you'd include s = 11 and s = 12, because both are not equal to two!

Post a Comment for "Deleting Elements From A List If They Do Not Follow 'if' 'or' Statements"