The Wayback Machine - https://web.archive.org/web/20250111011629/https://www.geeksforgeeks.org/python-program-to-find-the-string-in-a-list/
Open In App

Python program to find String in a List

Last Updated : 07 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Searching for a string in a list is a common operation in Python. Whether we’re dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, we’ll explore several methods to find a string in a list, starting from the most efficient to the least.

Using the in operator

The ‘in’ operator is the simplest and fastest way to check if a string exists in a list. It is efficient and widely used for this purpose.

Python
a = ['Learn', 'Python', 'With', 'GFG']

if 'Python' in a:
    print("String found!")
else:
    print("String not found!")

Output
String found!

Explanation:

  • The ‘in’ operator checks if the string exists in the list.
  • This approach is optimized for fast membership testing, making it ideal for most use cases.

Let’s explore the different ways to write a python program to find string in a list.

Using list.index()

The list.index() method returns the index of the first occurrence of the string in the list. If the string is not present, it raises an exception.

Python
a = ['Learn', 'Python', 'with', 'GFG']

try:
    index = a.index('Python')
    print("String found at index", index)
except ValueError:
    print("String not found!")

Output
String found at index 1

Explanation:

  • The list.index() method searches for the string and returns its index.
  • It requires exception handling using try and except in case the string is not found.

Using a for loop

A for loop provides a manual way to iterate through the list and compare each element with the target string.

Python
a = ['Learn', 'Python', 'With', 'GFG']

found = False
for item in a:
    if item == 'Python':
        found = True
        break

if found:
    print("String found!")
else:
    print("String not found!")

Output
String found!

Explanation:

  • The for loop iterates through the list and checks each element against the target string.
  • While slower than the in operator, it gives us a greater control over the process.

Using list comprehension

List comprehensions allow us to create a new list containing matching elements. This is useful if we need more than just checking for existence.

Python
a = ['Learn', 'Python', 'With', 'GFG']

found = [item for item in a if item == 'Python']
if found:
    print("String found!")
else:
    print("String not found!")

Output
String found!

Explanation:

  • List comprehension creates a new list of elements matching the target string.
  • It is less efficient because it creates an intermediate list.

Using filter function

TheThe can be used to filter elements based on a condition, returning a matching subset of the list.

Python
a = ['Learn', 'Python', 'With', 'GFG']

found = list(filter(lambda x: x == 'Python', a))
if found:
    print("String found!")
else:
    print("String not found!")

Output
String found!

Explanation:

  • The filter function applies a condition to each element of the list and returns matches.
  • It is the least efficient because it creates an intermediate object and processes every element.

Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article

Similar Reads

three90RightbarBannerImg