-2

While I need to test a condition whether some strings are existing in the list or not, I'm getting always same answer.

Like in below list I need to check for string "Inactive" or "Dead", if found print something.

list = [u'\u25cf multi-user.target - Multi-User System\n', u'   Loaded: loaded (/lib/systemd/system/multi-user  .target; static; vendor preset: enabled)\n', u'   Active: active since Mon 2017-02-20 20:58:28 HKT; 1h 20min ago\n  ', u'     Docs: man:systemd.special(7)\n', u'\n', u'Feb 20 20:58:28 ubuntu systemd[1]: Reached target Multi-User S  ystem.\n']

Code:

if 'Inactive' or 'Dead' in list:
  print "Service not active"

Tried pouring the output to a file, variable etc, but nothing seemed to work. Have already gone through solutions over the web but no luck. Please help.

jagatjyoti
  • 699
  • 3
  • 10
  • 29
  • 5
    that condition is not doing what you think it is doing – depperm Feb 20 '17 at 15:20
  • 1
    `if 'Inactive' or 'Dead' in list:` **does not** mean `if 'Inactive' in list or 'Dead' in list:` and always returns `True` because `bool('Inactive') = True` – Ma0 Feb 20 '17 at 15:21
  • 1
    `if any(i in list for i in ['Inactive', 'Dead']):` is what you could use. Please see the above link to see why your current condition is wrong. – Cory Kramer Feb 20 '17 at 15:23

1 Answers1

0

you have to check each item individually if it is not a 100% match ie

for x in list:
    if whatever in x:
        print("found whatever")

Also, you should pick a different name other than 'list' since it shadows the builtin list

user2682863
  • 3,097
  • 1
  • 24
  • 38