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

Here, we have a list of tuples and we need to remove the given character from the first element of the tuple from each tuple of the list in Python.
Submitted by Shivang Yadav, on August 24, 2021

We have a list of tuples where the first element of each tuple contains a character that is not required in the tuple. We need to remove that given character from the first element of the tuple from each tuple of the list in python.

Before proceeding further, let's recap some topics that we will need for solving the problem.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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)]

Solution:

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)]

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.