Q:

Python program to extract digits from tuple list

belongs to collection: Python Tuple Programs

0

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,

All Answers

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

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:

# Python program to extract digits from tuple list

from itertools import chain

# Creating the list
myList = [(4, 62), (2, 65), (5, 9), (0,1)]
print("The list is : " + str(myList))

# Extract digits from Tuple list
valMap = map(lambda ele: str(ele), chain.from_iterable(myList))
uniqueDigits = set()
for values in valMap:
	for digits in values:
		uniqueDigits.add(int(digits))

# Printing the set
print("Unique digits of the set : " + str(uniqueDigits))

Output:

The list is : [(4, 62), (2, 65), (5, 9), (0, 1)]
Unique digits of the set : {0, 1, 2, 4, 5, 6, 9}

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:

# Python program to extract digits from tuple list

import re

# Creating the list
myList = [(4, 62), (2, 65), (5, 9), (0,1)]
print("The list is : " + str(myList))

# Extract digits from Tuple list
digitString = re.sub(r'[\[\]\(\), ]', '', str(myList))
uniqueDigits = set()
for digits in digitString:
    uniqueDigits.add(int(digits))

# Printing the set
print("Unique digits of the set : " + str(uniqueDigits))

Output:

The list is : [(4, 62), (2, 65), (5, 9), (0, 1)]
Unique digits of the set : {0, 1, 2, 4, 5, 6, 9}

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 find all pairs combination of tw... >>
<< Python program to remove all tuples of length K...