mercredi 7 novembre 2018

Evaluate once if string not in item when iterating over list - python 3.x

I want to use this in an if statement so it would be useful if the output is a bollean expression but is there an easy way to find if string is not found in an item within a list?

e.g.

string_foo='teen'

list_foo=['one','two',three']

the problem is i need a loop to output somthing if a string is not found within an item in a list, the problem i have with doing it this way:

[print('no') if string_foo not in i for i in list_foo]

Is that for every item where string_foo isnt in i, it will print 'no' whereas i want it to only print 'no' once if string_foo isnt found within an item in list_foo

Aucun commentaire:

Enregistrer un commentaire