0
if 'PASSED' in cell.value or 'FAILED' in cell.value or 'INVALID' in cell.value:
    Test.append(6)

What is a more concise way to do this? I want to make it do something like

if cell.value in ('PASSED','INVALID', 'FAILED')

but that doesn't work

rishubk
  • 441
  • 2
  • 8
  • 19
  • possible duplicate of [How to check if one of the following items is in a list?](http://stackoverflow.com/questions/740287/how-to-check-if-one-of-the-following-items-is-in-a-list) – alexo_o Jul 24 '15 at 18:57
  • Why does this ```'hello' in ( 'hello', 'world' ) #returns True``` not work for you? Are you sure that ```cell.value``` is a string ? – dermen Jul 24 '15 at 18:58
  • 1
    If cell.value is a string then "if cell.value in ('PASSED','INVALID', 'FAILED'):" will absolutely work - what error did you get? – gkusner Jul 24 '15 at 19:03
  • @dermen: clearly `cell.value` is a *larger* string which may *contain* any of those three strings. – Martijn Pieters Jul 24 '15 at 21:02
  • @gkusner: that'll only work if `cell.value` is *exactly* one of those strings, but not if `cell.value` *contains* such a string; e.g. `cell.value` is a longer string like `"The tests results were INVALID"`. `'INVALID' in cell.value` is then true, but your test is not. – Martijn Pieters Jul 24 '15 at 21:04
  • @MartijnPieters, clear from the first section of code, but not from the second. But I guess one can infer the first section of code works implying cell.value is a larger string.. still a little unclear to me – dermen Jul 24 '15 at 21:07
  • @dermen: the second line doesn't work because the strings are larger, I'm not sure why it isn't clear. – Martijn Pieters Jul 24 '15 at 21:09
  • @MartijnPieters ```"but that doesn't work"``` referring to the second line doesn't directly imply the first line of code works is all im saying – dermen Jul 24 '15 at 21:11
  • I guess the bottom line is cell.value is ambiguous here, and probably very easy to paste into the question post, especially since its not being described anywhere – dermen Jul 24 '15 at 21:12
  • sorry for being unclear, but yes cell.value is a larger string, so the answer below works. thanks – rishubk Jul 24 '15 at 21:50

1 Answers1

3

Use the any() function and a generator expression:

if any(s in cell.value for s in ('PASSED','INVALID', 'FAILED')):

This tests each string in turn, returning True as soon as a test passes.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343