Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- list_to_search = ["Birding", "Large Bird Handling"]
- search_key = "Bird"
- # first list comprehension will match *any* location
- # case-sensitive
- result_any_position_case_sensitive = [item for item in list_to_search if search_key in item]
- # case-insensitive
- result_any_position = [item for item in list_to_search if search_key.lower() in item.lower()]
- # second list comprehension will match only at the beginning of each word
- # regex is the better solution for this
- # case sensitive
- result_beginning_of_word_case_sensitive = [item for item in list_to_search if item.startswith(search_key)]
- # case insensitive
- result_beginning_of_word = [item for item in list_to_search if item.lower().startswith(search_key.lower())]
Add Comment
Please, Sign In to add comment