Python program to extract tuples with all numeric strings

Here, we are given a list of tuples with string elements. We need to extract tuples with all numeric strings in Python.
Submitted by Shivang Yadav, on August 28, 2021

We will use a list of tuples with all elements of the tuple as string values. And we will be extracting all the tuples from the list whose elements consist of string elements which are numeric values.

Before proceeding further, let's recap some topics from the Python programming language that we need to implement the solution of this 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]

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.

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

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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