chainsol icon

Untitled

chainsol | PRO | 12/22/17 04:52:02 PM UTC | 0 ⭐ | 458 👁️ | Never ⏰ | []
Python |

710 B

|

None

|

0 👍

/

0 👎

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())]

Comments