AKIWEB
AKIWEB

Reputation: 19632

How to compare strings in python in List?

I have recently started working wth Python. I need to compare strings in Python in the List and I am not sure how this will work out -

Below is my code -

def my_func ( event ):
  print event
  print (event.type)
  if event.type == EventType.CHILD:
    children=zk.get_children("/ss/testing", watch=my_func)
    print(children)

print(children1) will print out something like this, if it has one children or two or three-

[u'test1']

or

[u'test1', u'test2']

or

[u'test1', u'test2', u'test3']

I need to check whether children contains workflow string or not. Right now it only contains test1, test2, test3 but in future it can have workflow as well like test1, test2, test3, workflow

If it contains workflow. Then I will print out only workflow and nothing else.

NOTE:- get_children returns List as shown in the documentation I guess

Any idea how this can be done?

UPDATE:-

If workflow nodes gets added up then it will show like this if I print out children-

[u'test1', u'test2', u'test3', u'workflow']

So I need to check whether children contains workflow or not, if it doesn't contains, then we won't do anything but if it contains, then we will print out workflow only not test1, test2, test3 after extracting from it.

Upvotes: 2

Views: 282

Answers (2)

Silas Ray
Silas Ray

Reputation: 26160

You can use an in line if statement.

print('workflow' if 'workflow' in children else children)

Upvotes: 2

slider
slider

Reputation: 12990

'workflow' in children

returns True/False if it is/isn't in children

Then, your code would be:

if 'workflow' in children: print 'workflow'

Upvotes: 4

Related Questions