Python program to extract digits from tuple list

Here, we have a list of tuples and we need to create a program in Python to extract digits from the tuple list and print them.
By Shivang Yadav Last updated : December 12, 2023

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)

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: Using map() Method, Lambda Expression, and Loop

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.

Read about: Python map() function, Lambda Expression, and Python loops

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: Using Regular Expressions

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}

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






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