If any item of list starts with string?

tkbx picture tkbx · Oct 8, 2012 · Viewed 22.8k times · Source

I'm trying to check is any item of a list starts with a certain string. How could I do this with a for loop? IE:

anyStartsWith = False
for item in myList:
    if item.startsWith('qwerty'):
        anyStartsWith = True

Answer

Ashwini Chaudhary picture Ashwini Chaudhary · Oct 8, 2012

Use any():

any(item.startswith('qwerty') for item in myList)