Python – Remove given character from first element of Tuple
Last Updated :
04 May, 2023
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
test_list = [( "GF ! g !" , 5 ), ( "! i ! s" , 4 ), ( "best !!" , 10 )]
print ( "The original list is : " + str (test_list))
K = "!"
res = [(sub[ 0 ].replace(K, ''), sub[ 1 ]) for sub in test_list]
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
test_list = [( "GF ! g !" , 5 ), ( "! i ! s" , 4 ), ( "best !!" , 10 )]
print ( "The original list is : " + str (test_list))
K = "!"
res = [(sub[ 0 ].translate({ ord (K): None }), sub[ 1 ]) for sub in test_list]
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
test_list = [( "GF!g!" , 5 ), ( "!i!s" , 4 ), ( "best!!" , 10 )]
print ( "The original list is : " + str (test_list))
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)
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:
- Initiate a for loop to traverse the list of tuples.
- Split each first element of a tuple by K and join by an empty string.
- Append the joined string and the second element of the tuple together as a tuple to output list.
- Display output list.
Python3
test_list = [( 'GF$g!' , 5 ), ( '!i$s' , 4 ), ( 'best!$' , 10 )]
print ( "The original list is : " + str (test_list))
K = "$"
res = []
for i in test_list:
v = []
x = i[ 0 ].split(K)
x = "".join(x)
v.append((x, i[ 1 ]))
res.append(v)
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:
- Define a list of tuples test_list.
- Use the map() function with a lambda function to remove the given character from the first element of each tuple.
- 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.
- The resulting filtered tuples are stored in a list filtered_list.
- The filtered list is printed.
Python3
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).
Similar Reads
Get first element from a List of tuples - Python
The goal here is to extract the first element from each tuple in a list of tuples. For example, given a list [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby')], we want to retrieve [1, 2, 3], which represents the first element of each tuple. There are several ways to achieve this, each varying in terms of
2 min read
Python | Remove given element from the list
Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Removing Tuples from a List by First Element Value - Python
In this problem we need to delete tuples based on a specific condition related to their first element. For example: We are given the list data = [("GeeksforGeeks", "Python", 1000), ("CodingForAll", "Java", 1200)] and we need to remove all tuples where the first element is "GeeksforGeeks", the desire
3 min read
Python | Remove given character from Strings list
Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read
Python program to remove last element from Tuple
Given a tuple, the task is to write a Python program to delete the last element in the tuple in Python. Example: Input: ("geeks", "for", "geeks") Output:("geeks", "for") Explanation: Here we are deleting the last element of the tuple and finally modifying the original one. Note: Tuples are immutable
3 min read
Python | Remove tuple from list of tuples if not containing any character
Given a list of tuples, the task is to remove all those tuples which do not contain any character value. Example: Input: [(', ', 12), ('...', 55), ('-Geek', 115), ('Geeksfor', 115),] Output: [('-Geek', 115), ('Geeksfor', 115)] Method #1 : Using list comprehension C/C++ Code # Python code to remove a
4 min read
Python - Extract only characters from given string
To extract only characters (letters) from a given string we can use various easy and efficient methods in Python. Using str.isalpha() in a Loop str.isalpha() method checks if a character in a string is an alphabetic letter. Using a loop, we can iterate through each character in a string to filter ou
2 min read
Getting an Element from Tuple of Tuples in Python
Tuples in Python are versatile data structures that allow you to store and organize collections of elements. When dealing with tuples of tuples, accessing specific elements becomes essential for effective data manipulation. In this article, we'll explore some different methods to retrieve elements f
2 min read
Python - Remove particular data type Elements from Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to remove particular data type elements from tuple. This kind of problem can occur in domains which require data preprocessing. Let's discuss certain ways in which this task can be performed. Input : test_tuple = (4,
6 min read
Python | Sort tuple based on occurrence of first element
Given a list of tuples, write a Python program to sort the list based on the occurrence of first element of tuples. Examples: Input : [(1, 'Jake'), (2, 'Bob'), (1, 'Cara')] Output : [(1, 'Jake', 'Cara', 2), (2, 'Bob', 1)] Input : [('b', 'ball'), ('a', 'arm'), ('b', 'b'), ('a', 'ant')] Output : [('a'
2 min read