Open In App
Related Articles

Python – Remove given character from first element of Tuple

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a Tuple list, remove K character from 1st element of the Tuple being String.

Input : test_list = [("GF$g!", 5), ("!i$s", 4), ("best!$", 10)], K = '$' 
Output : [('GFg!', 5), ('!is', 4), ('best!', 10)] 
Explanation : First element's strings K value removed. 
Input : test_list = [("GF$g!", 5), ("best!$", 10)], K = '$' 
Output : [('GFg!', 5), ('best!', 10)] 
Explanation : First element's strings K value removed.

Method #1 : Using replace() + list comprehension

In this, we use replace() to perform the task of removing of K character and list comprehension to reform the tuple.

Python3

# Python3 code to demonstrate working of
# Remove K character from first element of Tuple
# Using replace() + list comprehension
 
# Initializing list
test_list = [("GF ! g !", 5), ("! i ! s", 4), ("best !!", 10)]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = "!"
 
# Replace with empty string removes the desired char.
res = [(sub[0].replace(K, ''), sub[1]) for sub in test_list]
 
# Printing result
print("The filtered tuples : " + str(res))

                    

Output
The original list is : [('GF!g!', 5), ('!i!s', 4), ('best!!', 10)]
The filtered tuples : [('GFg', 5), ('is', 4), ('best', 10)]

Method #2 : Using translate() + list comprehension

In this, we perform the task of removal using translate(), which needs conversion to ASCII using ord(), and replaced with an empty character.

Python3

# Python3 code to demonstrate working of
# Remove K character from first element of Tuple
# Using translate() + list comprehension
 
# Initializing list
test_list = [("GF ! g !", 5), ("! i ! s", 4), ("best !!", 10)]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = "!"
 
# Translation after conversion to ascii number
res = [(sub[0].translate({ord(K): None}), sub[1]) for sub in test_list]
 
# Printing result
print("The filtered tuples : " + str(res))

                    

Output
The original list is : [('GF!g!', 5), ('!i!s', 4), ('best!!', 10)]
The filtered tuples : [('GFg', 5), ('is', 4), ('best', 10)]

Method #3: Using for loop

Python3

# Python3 code to demonstrate working of
# Remove K character from first element of Tuple
 
# Initializing list
test_list = [("GF!g!", 5), ("!i!s", 4), ("best!!", 10)]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
res1 = []
 
K = "!"
 
for i in test_list:
    res = ""
    for j in i[0]:
        if(j != K):
            res += j
    x = (res, i[1])
    res1.append(x)
 
 
# Printing result
print("The filtered tuples : " + str(res1))

                    

Output
The original list is : [('GF!g!', 5), ('!i!s', 4), ('best!!', 10)]
The filtered tuples : [('GFg', 5), ('is', 4), ('best', 10)]

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

Method #4 : Using split() and join() methods

Approach:

  1. Initiate a for loop to traverse the list of tuples.
  2. Split each first element of a tuple by K and join by an empty string.
  3. Append the joined string and the second element of the tuple together as a tuple to output list.
  4. Display output list.

Python3

# Python3 code to demonstrate working of
# Remove K character from first element of Tuple
 
# Initializing list
test_list = [('GF$g!', 5), ('!i$s', 4), ('best!$', 10)]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = "$"
 
# Replacing with empty string removes the desired char.
res = []
for i in test_list:
 
    v = []
    x = i[0].split(K)
    x = "".join(x)
    v.append((x, i[1]))
    res.append(v)
 
 
# Printing result
print("The filtered tuples : " + str(res))

                    

Output
The original list is : [('GF$g!', 5), ('!i$s', 4), ('best!$', 10)]
The filtered tuples : [[('GFg!', 5)], [('!is', 4)], [('best!', 10)]]

Time Complexity: O(M*N) M- length of tuples list N – length of each tuple
Auxiliary Space: O(M*N) M- length of tuples list N – length of each tuple

METHOD 5:Using map and lambda method

The given Python code removes a given character from the first element of each tuple in a list using the map function and lambda function. It replaces the given character with an empty string and strips any whitespace from the resulting string.

Algorithm:

  1. Define a list of tuples test_list.
  2. Use the map() function with a lambda function to remove the given character from the first element of each tuple.
  3. The lambda function takes a tuple as input, replaces the given character with an empty string using the replace() method and strips any whitespace from the resulting string using the strip() method.
  4. The resulting filtered tuples are stored in a list filtered_list.
  5. The filtered list is printed.

Python3

# Input list
test_list = [("GFg !", 5), ("is", 4), ("best!", 10)]
 
filtered_list = list(
    map(lambda tup: (tup[0].replace('!', '').strip(), tup[1]), test_list))
 
print("The filtered tuples:", filtered_list)

                    

Output
The filtered tuples: [('GFg', 5), ('is', 4), ('best', 10)]

Time Complexity: O(N), where n is the number of tuples in the input list. This is because the map() function applies the lambda function to each tuple in the input list once.
Auxiliary Space: O(N), where n is the number of tuples in the input list. This is because the resulting filtered tuples are stored in a list, which has a space complexity of O(n).



Last Updated : 04 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads