Q:

Python program to extract tuples with all numeric strings

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]

Lambda function is an anonymous function - that means the function which does not have any name.

That is all the basic information required here. Now, let's get back to the problem and see the sample inputs and their outputs for a better understanding of the problem.

To solve this problem, we need to check if all the elements of tuples are strings that decode numbers. Then keep it, otherwise discard it from the resultant tuple list.

Python programming language allows us to solve this problem in multiple ways and also some built-in methods from the python library can be helpful in performing the task.

All Answers

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

Method 1:

One method to solve the problem is checking is the string decodes to number using isdigit() function and using list comprehension traverse the whole list and using all() check all elements of tuple satisfies the above condition.

Here's the program this will display the working of our solution,

# Python program to extract tuples 
# with all Numeric String 

# Creating and printing Tuple list 
tupList = [("4", "645"), ("python","54"), ("1921", "431"), ("includehelp", "90")]
print("The initial tuple list is " + str(tupList))

# Extracting all tuples which have numeric strings 
numTupList = [tup for tup in tupList if all(vals.isdigit() for vals in tup)]

# Printing extracted list 
print("List of tuples with all elements of list that are Numeric string : " + str(numTupList))

Output:

The initial tuple list is [('4', '645'), ('python', '54'), ('1921', '431'), ('includehelp', '90')]
List of tuples with all elements of list that are Numeric string : [('4', '645'), ('1921', '431')]

Method 2:

One more method to solve the problem is by checking Python's built-in methods to perform the task. The methods filter() along with lambda is used to extract elements and in lambda function isdigit() is used to check if the values are numeric values.

# Python program to extract tuples 
# with all Numeric String 

# Creating and printing Tuple list 
tupList = [("4", "645"), ("python","54"), ("1921", "431"), ("includehelp", "90")]
print("The initial tuple list is " + str(tupList))

# Extracting all tuples which have numeric strings 
numTupList = list(filter(lambda tup : all(vals.isdigit() for vals in tup), tupList))

# Printing extracted list 
print("List of tuples with all elements of list that are Numeric string : " + str(numTupList))

Output:

The initial tuple list is [('4', '645'), ('python', '54'), ('1921', '431'), ('includehelp', '90')]
List of tuples with all elements of list that are Numeric string : [('4', '645'), ('1921', '431')]

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 perform tuple intersection in li... >>
<< Python program to remove given character from the ...