Skip to content

Instantly share code, notes, and snippets.

@shau-lok
Created November 1, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shau-lok/9606c7b9cfa3515148e5cb6e53e6f672 to your computer and use it in GitHub Desktop.
Save shau-lok/9606c7b9cfa3515148e5cb6e53e6f672 to your computer and use it in GitHub Desktop.
Check if a Python list item contains a string inside another string
# Check if a Python list item contains a string inside another string
# If you only want to check for the presence of abc in any string in the list, you could try
some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
if any("abc" in s for s in some_list):
# whatever
# If you really want to get all the items containing abc, use
matching = [s for s in some_list if "abc" in s]
@FeliciaXmL
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment