Find a String in a List in Python

  • Post category:List

Finding a string in a list is a common task in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to search for a string efficiently.

Example 1:

fruits = ['apple', 'banana', 'kiwi']
search_string = 'banana'
found = search_string in fruits
print(found)

Output: True

Example 2:

names = ['Alice', 'Bob', 'Charlie']
search_name = 'David'
found = any(search_name == name for name in names)
print(found)

Output: False