We can use Python Show
Output:
Recommended Reading: Python f-strings Let’s look at another example where we will ask the user to enter the string to check in the list.
Output:
Python Find String in List using count()We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list.
Finding all indexes of a string in the listThere is no built-in function to get the list of all the indexes of a string in the list. Here is a simple program to get the list of all the indexes where the string is present in the list.
Output: You can checkout complete python script and more Python examples from our GitHub Repository. What would be the best way to find the index of a specified character in a list containing multiple characters? asked Oct 2, 2010 at 20:48
a sandwhicha sandwhich 4,25412 gold badges40 silver badges61 bronze badges
If the list is already sorted, you can of course do better than linear search. answered Oct 2, 2010 at 20:54
2 Probably the
answered Oct 2, 2010 at 20:54
Jim BrissomJim Brissom 30.5k3 gold badges37 silver badges33 bronze badges As suggested by others, you can use
answered Oct 2, 2010 at 21:09
answered Mar 6, 2021 at 5:43
1 In this article, we’ll take a look at how we can find a string in a list in Python. There are various approaches to this problem, from the ease of use to efficiency. We can use Python’s in operator to find a string in a list in Python. This takes in two operands Here, We can directly use this operator in the following way: Output We can also convert this into a function, for ease of use. def check_if_exists(x, ls): if x in ls: print(str(x) + ' is inside the list') else: print(str(x) + ' is not present in the list') ls = [1, 2, 3, 4, 'Hello', 'from', 'AskPython'] check_if_exists(2, ls) check_if_exists('Hello', ls) check_if_exists('Hi', ls) Output 2 is inside the list Hello is inside the list Hi is not present in the list This is the most commonly used, and recommended way to search for a string in a list. But, for illustration, we’ll show you other methods as well. Using List ComprehensionLet’s take another case, where you wish to only check if the string is a part of another word on the list and return all such words where your word is a sub-string of the list item. Consider the list below: ls = ['Hello from AskPython', 'Hello', 'Hello boy!', 'Hi'] If you want to search for the substring ls = ['Hello from AskPython', 'Hello', 'Hello boy!', 'Hi'] matches = [match for match in ls if "Hello" in match] print(matches) This is equivalent to the below code, which simply has two loops and checks for the condition. ls = ['Hello from AskPython', 'Hello', 'Hello boy!', 'Hi'] matches = [] for match in ls: if "Hello" in match: matches.append(match) print(matches) In both cases, the output will be: ['Hello from AskPython', 'Hello', 'Hello boy!'] As you can observe, in the output, all the matches contain the string Using the ‘any()’ methodIn case you want to check for the existence of the input string in any item of the list, We can use the any() method to check if this holds. For example, if you wish to test whether ‘AskPython’ is a part of any of the items of the list, we can do the following: ls = ['Hello from AskPython', 'Hello', 'Hello boy!', 'Hi'] if any("AskPython" in word for word in ls): print('\'AskPython\' is there inside the list!') else: print('\'AskPython\' is not there inside the list') Output 'AskPython' is there inside the list! Using filter and lambdasWe can also use the ls = ['Hello from AskPython', 'Hello', 'Hello boy!', 'Hi'] # The second parameter is the input iterable # The filter() applies the lambda to the iterable # and only returns all matches where the lambda evaluates # to true filter_object = filter(lambda a: 'AskPython' in a, ls) # Convert the filter object to list print(list(filter_object)) Output We do have what we expected! Only one string matched with our filter function, and that’s indeed what we get! ConclusionIn this article, we learned about how we can find a string with an input list with different approaches. Hope this helped you with your problem! References
How do I find a specific word in a list Python?Use the filter() Function to Find Elements From a Python List Which Contain a Specific Substring. The filter() function retrieves a subset of the data from a given object with the help of a function. This method will use the lambda keyword to define the condition for filtering data.
How do I identify a specific letter in Python?Using Python's "in" operator
The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .
How do you check if a list contains a particular string in Python?To check if the list contains an element in Python, use the “in” operator. The “in” operator checks if the list contains a specific item or not. It can also check if the element exists on the list or not using the list.
|