0

Im having an issue with the order of operation in python see below code. I have a nested if that I would like condensed down to one line.

The below code reads if "a" and "b" and "c" in list pass or if "c" is in list pass too regardless of abc. I want the code to read if "a" and "b" in list and either "c" or "d" is in list pass

if "a" and "b" in list:
    if "c" or "d" in list:
        print()

if "a" and "b" in list and "c" or "d" in list:
    print()
    # keyerror "d" not in list
mRyan
  • 332
  • 4
  • 18
  • 1
    See also: [Why does \`a == b or c or d\` always evaluate to True?](//stackoverflow.com/q/20002503) – iBug Jan 02 '19 at 13:39
  • 1
    The dupe's useful... but if you're familiar with sets, then you might want to consider `if {'a', 'b'}.issubset(your_list) and {'c', 'd'}.intersection(your_list)` which you might be able to translate to simple set operations starting with `{'a', 'b', 'c', 'd'}.intersection(your_list)...` then work with that intersection... – Jon Clements Jan 02 '19 at 13:43
  • Try: `("c" or "d") in list` – Sumit S Chawla Jan 02 '19 at 13:44
  • @Sam nope - that's just compounding the issue the OP is having here... that equates to `'c' in list`... – Jon Clements Jan 02 '19 at 13:44

0 Answers0