How to remove list elements that contain given String in Python

If you are looking for a tutorial topic in Python, I suggest the topic Remove list elements that contain given String in Python. I use the str.find() function in combination with the list.append() function, list comprehension in combination with search functions, and use for loop. Post details below.

Remove list elements that contain given String in Python.

Use the re.search function in conjunction with a list comprehension.

Syntax:

re.search(pattern, str)

Parameters:

  • pattern: is RegEx.
  • str: search string.

Example:

  • List comprehension iterates through the objects in the list and the re.search() function checks the condition.
  • Any element that does not have the same character as the substring will not be deleted.
import re

testList = ['this is', 'a test', 'program']
print('The original list is :', testList)

subStr = 'hi'

# Use the re.search() and list comprehension to remove strings in a list containing substrings.
result = [x for x in testList if not re.search(subStr, x)]
print('List after removing element:', result)

Output:

The original list is : ['this is', 'a test', 'program']
List after removing element: ['a test', 'program']

Use for loop.

Syntax:

list.remove(The object is deleted from the list)

The remove() method in Python removes the obj object from the list. This method returns no value but deletes the given object from the list.

Example:

  • Create a list of strings and substrings.
  • The for loop iterates over the elements in the list, using the in operator to check if the substring is present in the element being iterated over and calls the list.remove() function to remove those elements.
testList = ['This is', 'a test', 'program']
print('The original list is :', testList)

# Create a substring
subStr = 'hi'

# The remove() method to remove strings in a list containing substrings.
for i in testList:
    if subStr in i:
        testList.remove(i)

print('List after removing element:', testList)

Output:

The original list is : ['This is', 'a test', 'program']
List after removing element: ['a test', 'program']

Use list comprehension.

Example:

  • The idea of ​​list comprehension is similar to a loop, but a list makes the code faster.
testList = ['this is', 'a test', 'program']
print('The original list is :', testList)

subStr = 'hi'

# Use list comprehension to remove strings in a list containing substrings.
result = [i for i in testList if subStr not in i]
print('List after removing element:', result)

Output:

The original list is : ['this is', 'a test', 'program']
List after removing element: ['a test', 'program']

Use the find() method.

Syntax:

str.find(str, start, end)

Parameters:

  • str: Specifies the search string.
  • start: The starting index. The default is 0.
  • end: End index. The default is the string length.

Syntax:

list.append()

The list.append() will add the element to the end of the list.

Example:

  • The str.find() function finds the elements containing the string in the list. The function returns a value of -1 which means it finds elements that do not contain a substring in the list.
  • Use the list.append() function to add those elements (elements that do not contain substrings) to the initialized empty list.
testList = ['this is', 'a test', 'program']
print('The original list is :', testList)

subStr = 'hi'
emptyList = []

# Use the str.find() and list.append() to remove strings in a list containing substrings.
for i in testList:
    if i.find(subStr) == -1:
        emptyList.append(i)

print('List after removing element:', emptyList)

Output:

The original list is : ['this is', 'a test', 'program']
List after removing element: ['a test', 'program']

Summary:

My article remove list elements that contain given String in Python would like to stop here. Hopefully, through the article, you will have ideas for this topic. I recommend using list comprehension to remove list elements that contain given String. Thank you for taking the time to read the article.

Leave a Reply

Your email address will not be published. Required fields are marked *