Search code examples
pythonliststartswith

If any item of list starts with string?


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

Solution

  • Use any():

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