Home > OS >  Is there a way to combine the following conditions in a shorter way?
Is there a way to combine the following conditions in a shorter way?

Time:11-10

The following code works but I wonder if there's a way to combine the following conditions in a shorter way?

xlist = ['a', 'b', 'c']
xlist[:] = [x for x in xlist if ('a' not in x) & ('b' not in x)]

I tried this but it didn't work

xlist[:] = [x for x in xlist if ['a', 'b'] not in x]

CodePudding user response:

if the element in the xlist is like above,then:

[x for x in xlist if x not in 'ab']

CodePudding user response:

You could try something like this:

xlist[:] = [x for x in xlist if all(i not in x for i in ['a', 'b'])]

This makes little difference if you're only looking to exclude a list of two things - but if you had a much longer list of strings you wanted to exclude, this could be a way to do it.

  • Related