AND/OR in Python?

Question:

I know that the and and or expressions exist in python, but is there any and/or expression? Or some way to combine them in order to produce the same effect as a and/or expression?

my code looks something like this:

if input=="a":        
    if "a" or "á" or "à" or "ã" or "â" in someList:            
        someList.remove("a") or someList.remove("á") or someList.remove("à") or someList.remove("ã") or someList.remove("â")

with this, I mean that if the user inputs “a” and any type of “a” is included in a previously defined list, can I have all the types of “a” removed from a given list?

python tells me that there is a problem in:

someList.remove("a") or someList.remove("á") or someList.remove("à") or someList.remove("ã") or someList.remove("â")

he tells me: ValueError: list.remove(x): x not in list

Asked By: JNat

||

Answers:

or is not exclusive (e.g. xor) so or is the same thing as and/or.

Answered By: Matt Ball

Are you looking for…

a if b else c

Or perhaps you misunderstand Python’s or? True or True is True.

Answered By: Amber

x and y returns true if both x and y are true.
x or y returns if either one is true.

From this we can conclude that or contains and within itself unless you mean xOR (or except if and is true)

Answered By: user1241335

As Matt Ball‘s answer explains, or is “and/or”. But or doesn’t work with in the way you use it above. You have to say if "a" in someList or "á" in someList or.... Or better yet,

if any(c in someList for c in ("a", "á", "à", "ã", "â")):
    ...

That’s the answer to your question as asked.

Other Notes

However, there are a few more things to say about the example code you’ve posted. First, the chain of someList.remove... or someList remove... statements here is unnecessary, and may result in unexpected behavior. It’s also hard to read! Better to break it into individual lines:

someList.remove("a")
someList.remove("á")
...

Even that’s not enough, however. As you observed, if the item isn’t in the list, then an error is thrown. On top of that, using remove is very slow, because every time you call it, Python has to look at every item in the list. So if you want to remove 10 different characters, and you have a list that has 100 characters, you have to perform 1000 tests.

Instead, I would suggest a very different approach. Filter the list using a set, like so:

chars_to_remove = set(("a", "á", "à", "ã", "â"))
someList = [c for c in someList if c not in chars_to_remove]

Or, change the list in-place without creating a copy:

someList[:] = (c for c in someList if c not in chars_to_remove)

These both use list comprehension syntax to create a new list. They look at every character in someList, check to see of the character is in chars_to_remove, and if it is not, they include the character in the new list.

This is the most efficient version of this code. It has two speed advantages:

  1. It only passes through someList once. Instead of performing 1000 tests, in the above scenario, it performs only 100.
  2. It can test all characters with a single operation, because chars_to_remove is a set. If it chars_to_remove were a list or tuple, then each test would really be 10 tests in the above scenario — because each character in the list would need to be checked individually.
Answered By: senderle

For the updated question, you can replace what you want with something like:

someList = filter(lambda x: x not in ("a", "á", "à", "ã", "â"), someList)

filter evaluates every element of the list by passing it to the lambda provided. In this lambda we check if the element is not one of the characters provided, because these should stay in the list.

Alternatively, if the items in someList should be unique, you can make someList a set and do something like this:

someList = list(set(someList)-set(("a", "á", "à", "ã", "â")))

This essentially takes the difference between the sets, which does what you want, but also makes sure every element occurs only once, which is different from a list. Note you could store someList as a set from the beginning in this case, it will optimize things a bit.

Answered By: KillianDS
if input == 'a':
    for char in 'abc':
        if char in some_list:
            some_list.remove(char)
Answered By: doda

Try this solution:

for m in ["a", "á", "à", "ã", "â"]:
    try:
        somelist.remove(m)
    except:
        pass

Just for your information. and and or operators are also using to return values. It is useful when you need to assign value to variable but you have some pre-requirements

operator or returns first not null value

#init values
a,b,c,d = (1,2,3,None)

print(d or a or b or c)
#output is value of variable `a` - 1

print(b or a or c or d)
#output is value of variable `b` - 2

Operator and returns last value in the sequence if any of the members don’t have None value. If they have at least one None value we get is None

print(a and d and b and c)
#output: None

print(a or b or c)
#output is value of c - 3
Answered By: Roman Kazakov
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.