Finding a string in a list in Python
This tutorial shows you how to find elements from a Python list that have a specific substring in them.
We will use the following list and extract ack
the strings that have in it.
my_list = ["Jack", "Mack", "Jay", "Mark"]
for
Find elements containing a specific substring from a list
using loop in Python
In this method, we iterate through the list and check if the substring exists in an element. If the substring exists in that element, then we store it in the string. The following code shows how to do it.
str_match = [s for s in my_list if "ack" in s]
print(str_match)
Output:
['Jack', 'Mack']
in
The keyword checks if the given string, in this case "ack"
, exists in the string. You can also use __contains__
the method instead, which is a magic method of the string class. For example:
str_match = [s for s in my_list if s.__contains__("ack")]
print(str_match)
Output:
['Jack', 'Mack']
Use filter()
the function to find elements containing a specific substring from a Python list
filter()
The function retrieves a subset of data from a given object with the help of a function. The method will use lambda
the keyword to define the conditions for filtering the data. lambda
The keyword creates a one-line function in Python lambda
. Take a look at the following code snippet.
str_match = list(filter(lambda x: "ack" in x, my_list))
print(str_match)
Output:
['Jack', 'Mack']
Find elements containing a specific substring from a Python list using regular expressions
A regular expression is a sequence of characters that can be used as a matching pattern to search for elements. To use regular expressions, we have to import re
the module. In this method, we will use for
the loop and re.search()
the method, which is used to return the elements that match a specific pattern. The following code will explain how to do it.
import re
pattern = re.compile(r"ack")
str_match = [x for x in my_list if re.search("ack", x)]
print(str_match)
Output:
['Jack', 'Mack']
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to Find the Maximum Value in a List in Python
Publish Date:2025/05/09 Views:99 Category:Python
-
This tutorial will demonstrate how to find the maximum value in a list in Python. This tutorial will cover a number of scenarios and data types, from simple lists of integers to more complex structures like arrays within arrays. for Finding
Convert a list to a set in Python
Publish Date:2025/05/08 Views:145 Category:Python
-
This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values in sequence. Python list stores comma separated values between
Remove the first element from a list in Python
Publish Date:2025/05/08 Views:173 Category:Python
-
This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which
Convert a list to lowercase in Python
Publish Date:2025/05/08 Views:112 Category:Python
-
Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin
Remove all occurrences of an element from a Python list
Publish Date:2025/05/08 Views:69 Category:Python
-
In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element
Convert hex to bytes in Python
Publish Date:2025/05/08 Views:102 Category:Python
-
Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for
b in front of string in Python
Publish Date:2025/05/08 Views:54 Category:Python
-
This tutorial will discuss the statement in Python b" . b" Using the statement in Python b" The notation is used to specify strings in Python bytes . In contrast to regular strings with ASCII characters, bytes a string is an array of byte v
How to Convert Integer to Binary in Python
Publish Date:2025/05/08 Views:130 Category:Python
-
This tutorial explains how to convert an integer to binary in Python. This tutorial also lists some sample codes to illustrate different ways of converting from int to binary in Python. bin() Convert Int to Binary in Python using In Python,
How to convert an integer to bytes
Publish Date:2025/05/08 Views:77 Category:Python
-
Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in