Example:
tuple = ("python", "includehelp", 43, 54.23)
Extracting digits from a Tuple list
Python has many applications in today's world, from web to AI and data science, Python is everywhere. And the core collections of Python are utilised a lot to hold and manipulate data. This makes it very important to understand how to use this collection. And extraction of unique digits is one important thing that needs to be understood in order to function the program better.
We have a list of tuples that contains some integer values. And we need to extract all the unique digits from this list and store them in a list and then print it.
Example:
Input:
list = [(4, 62), (2, 65), (5, 9), (0,1)]
Output:
[4, 6, 2, 5, 9, 0, 1]
For this Python provides us multiple ways, let explore some of them,
Method 1:
We can extract unique digits by taking each value and then finding all the digits in each value and store in a collection. To make sure that the store digits are unique, we need to take a set.
Program:
Output:
In the above code we have first converted the list of tuples into a map of string value which is stored in the valMap variable. Then, we have extracted each number in string form from this map and from each string we have extracted digits. And then stored all digits to a set which will discard duplicate values.
Method 2:
Another method to solve the problem is to use regular expressions to extract all the digits from the list of tuples and then store them into a set to make sure that only unique values are stored and the rest are discarded.
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer