Q:

Python program to remove given character from the first element of Tuple

belongs to collection: Python Tuple Programs

0

Example:

tuple = ("python", "includehelp", 43, 54.23)

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets []

Example:

list = [3 ,1,  5, 7]

List of tuples is a list whose each element is a tuple.

Example:

tupList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]

List Comprehension: It is an easy and compact way of creating a list by performing multiple operations on a data structure.

Now, let's get back to our problem and see how to solve it?
Here are sample inputs and outputs for the problem to understand the program's working.

Input:
tupList:
[("py_t_hon", 4), ("p_ro_gra_m", 5)] char = '_'

Output:
[("python", 4), ("program", 5)]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

To remove the given character from the first element of each tuple. We need to traverse the list and for each tuple take the first elements and check for the presence of the given character in it, if it is present delete the character otherwise do nothing.

Deleting the character from the string can be done in multiple ways in python. We will be discussing some of them clubbed with other methods to perform the required task.

Method 1:

One way to find the solution is by using list comprehension to iterate over the list of tuples and then for the first element of each tuple, we will remove the character using replace() method.

replace() method takes in two parameters, first, the character to be replaced, and second will be the character that replaces it.

Syntax:

replace(charToBeRep, repChar)

Program to remove the given character from the first element of Tuple

# Program to remove the given character
# from first element of Tuple

# Creating and printing the List of Tuple 
tupList = [("py_t_hon", 4), ("p_ro_gra_m", 5)]
print("Initial List of Tuples : " + str(tupList))
char = "_"

convTupList = [(tup[0].replace(char, ''), tup[1]) for tup in tupList]

# printing the tuple list after removing character
print("List of tuples after removing character from first element : " + str(convTupList))

Output:

Initial List of Tuples : [('py_t_hon', 4), ('p_ro_gra_m', 5)]
List of tuples after removing character from first element : [('python', 4), ('program', 5)]

Method 2:

An alternate way to solve the problem is by replacing the replace() method with translate(). We will simply use list comprehension and use the translate() method in it to remove the character.

# Program to remove the given character 
# from first element of Tuple

# Creating and printing the List of Tuple 
tupList = [("py_t_hon", 4), ("p_ro_gra_m", 5)]
print("Initial List of Tuples : " + str(tupList))
char = "_"

convTupList = [(tup[0].translate({ord(char): None}), tup[1]) for tup in tupList]

# printing the tuple list after removing character
print("List of tuples after removing character from first element : " + str(convTupList))

Output:

Initial List of Tuples : [('py_t_hon', 4), ('p_ro_gra_m', 5)]
List of tuples after removing character from first element : [('python', 4), ('program', 5)]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to extract tuples with all numeric ... >>
<< Python program to check if any list element is pre...