Home » Python » Python programs

# 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.

Submitted by Shivang Yadav, on June 08, 2021

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:**

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}

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions