Sunday, December 29, 2024
Google search engine
HomeLanguagesPython | Remove trailing/leading special characters from strings list

Python | Remove trailing/leading special characters from strings list

Sometimes, while working with String lists, we can have a problem in which we need to perform the deletion of extra characters that can be termed as unwanted and occur at end of each string. Let’s discuss a way in which this task can be performed. 

Method 1: Using map() + str.strip() 
A combination of the above two functionalities can help us achieve this particular task. In this, we employ strip(), which has the ability to remove the trailing and leading special unwanted characters from string list. The map(), is used to extend the logic to each element in list. 

Python3




# Python3 code to demonstrate working of
# Remove trailing / leading special characters from strings list
# Using map() + str.strip()
 
# initializing list
test_list = ['\rgfg\t\n', 'is\n', '\t\tbest\r']
 
# printing list
print("The original list : " + str(test_list))
 
# Remove trailing / leading special characters from strings list
# Using map() + str.strip()
res = list(map(str.strip, test_list))
 
# Printing result
print("List after removal of special characters : " + str(res))


Output

The original list : ['\rgfg\t\n', 'is\n', '\t\tbest\r']
List after removal of special characters : ['gfg', 'is', 'best']

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. map() + str.strip() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method 2: Using split() and join() methods

Python3




# Python3 code to demonstrate working of
# Remove trailing / leading special characters from strings list
 
# initializing list
test_list = ['\rgfg\t\n', 'is\n', '\t\tbest\r']
 
# printing list
print("The original list : " + str(test_list))
 
# Remove trailing / leading special characters from strings list
res=[]
for i in test_list:
    res.append("".join(i.split()))
# Printing result
print("List after removal of special characters : " + str(res))


Output

The original list : ['\rgfg\t\n', 'is\n', '\t\tbest\r']
List after removal of special characters : ['gfg', 'is', 'best']

Method 3: Using regex
Explanation: Using the re module, we can use a regular expression to match and remove any trailing or leading special characters from the list of strings.

Python3




import re
 
test_list = ['\rgfg\t\n', 'is\n', '\t\tbest\r']
 
print("The original list : " + str(test_list))
 
# remove trailing/leading special characters using regex
res = [re.sub(r'^[^A-Za-z0-9]+|[^A-Za-z0-9]+$', '', i) for i in test_list]
 
# Printing result
print("List after removal of special characters : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : ['\rgfg\t\n', 'is\n', '\t\tbest\r']
List after removal of special characters : ['gfg', 'is', 'best']

Time complexity: O(n), where n is the length of the list
Auxiliary Space: O(n), where n is the length of the list

Method 3: Using list comprehension:

Python3




# initializing list
test_list = ['\rgfg\t\n', 'is\n', '\t\tbest\r']
# printing list
print("The original list : " + str(test_list))
# Using list comprehensions
res = [x.strip() for x in test_list]
# Printing result
print("List after removal of special characters : " + str(res))
#This code is contributed by Jyothi pinjala.


Output

The original list : ['\rgfg\t\n', 'is\n', '\t\tbest\r']
List after removal of special characters : ['gfg', 'is', 'best']

Time Complexity: O(N)
Auxiliary Space:  O(N)

Method#4: Using Recursive method.

Algorithm:

  1. If the input list is empty, return an empty list.
  2. Strip leading and trailing special characters from the first element of the list.
  3. Recursively, call strip_list_recursive function on the rest of the list (i.e., all elements except the first).
  4. Combine the stripped first element and the recursive result (i.e., stripped rest of the list) into a new list.
  5. Return the new list.
     

Python3




def strip_list_recursive(lst):
    if not lst:
        return lst
    else:
        first = lst[0].strip()
        rest = strip_list_recursive(lst[1:])
        return [first] + rest
# initializing list
test_list = ['\rgfg\t\n', 'is\n', '\t\tbest\r']
 
# printing list
print("The original list : " + str(test_list))
 
res = strip_list_recursive(test_list)
# Printing result
print("List after removal of special characters : " + str(res))
#this code contributed by tvsk


Output

The original list : ['\rgfg\t\n', 'is\n', '\t\tbest\r']
List after removal of special characters : ['gfg', 'is', 'best']

Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of any element in the list. This is because we need to call the strip() method on each element, which takes O(m) time, and we need to do this for each element in the list.
Auxiliary Space: O(n * m), where n is the length of the input list and m is the maximum length of any element in the list. This is because we are creating a new list of stripped elements, which takes up O(n * m) space in the worst case (when all elements in the list have maximum length m). Additionally, the recursive call stack can take up to O(n) space, since we need to make n recursive calls in the worst case (when the input list is not empty).

Method#5: Using a lambda function with the replace() method

In this method, we use a lambda function that takes each string in the test_list and uses the replace() method with the map() function to remove the special characters. The resulting list is then stored in res and printed using the print() function.

Python3




# initializing list
test_list = ['\rgfg\t\n', 'is\n', '\t\tbest\r']
 
# printing list
print("The original list : " + str(test_list))
 
# Remove trailing / leading special characters from strings list
# Using lambda function with replace() method
res = list(map(lambda x: x.replace('\n', '').replace('\t', '').replace('\r', ''), test_list))
 
# Printing result
print("List after removal of special characters : " + str(res))


Output

The original list : ['\rgfg\t\n', 'is\n', '\t\tbest\r']
List after removal of special characters : ['gfg', 'is', 'best']

Time complexity: O(n), where n is the length of the original list.
Auxiliary Space: O(n), where n is the length of the original list, because we are creating a new list with the same number of elements as the original list.

RELATED ARTICLES

Most Popular

Recent Comments