Signup/Sign In
PUBLISHED ON: AUGUST 11, 2021

Python Progarm to Extract Digits from Tuple List

Python can perform a wide range of data manipulation tasks. We have a case where we are provided a list with entries that are tuples of two integers. In this tutorial, we'll look at how to extract the digits from tuple items in a list by two different methods.

Need to Extract Digit from Tuples List

When dealing with Python lists, we may encounter an issue where we need to remove all of the numbers from a tuple list. This type of difficulty may be found in data domains and day-to-day programming. Let us have a look at the examples to understand the input-output format:

Input: List = [(25, 13), (22, 19)]

Output: [9, 5, 3, 1, 2]

Input: List = [(25, 13), (13, 19), (11, 10), (99, 12)]

Output: [0, 5, 3, 1, 2, 9]

To execute this task, we can follow multiple approaches, some are discussed below:

  1. Using Regex Expression

  2. Using Chain and Set

We will discuss all these approaches in detail separately.

Approach 1: Using Regex Expression

A regular expression (or RE) provides a collection of strings that match it; the methods in this module let you see if a string matches a regular expression. In this approach, the needed unique digits are extracted using an appropriate regex expression.

Algorithm

  1. Import Regex Expressions.
  2. Initialize the list
  3. Print the original list
  4. Extract the digit by Regex Expressions.
  5. Print Out the result

Program to Extract Digit from Tuples List

In this program, we create a regular expression to convert the tuples to normal strings and then use the set method to extract the unique numbers.

import re
List = [(25, 13), (22, 19)]
# Given list
print("Orignal list : ", List)
temp = re.sub(r'[\[\]\(\), ]', '', str(List))
# Using set
result =  [int(i) for i in set(temp)]
# Result
print("Digits Extracted: ",result)


Original list : [(25, 13), (22, 19)]
Digits Extracted: [1, 9, 3, 2, 5]

Approach 2: Using Chain and Sets

The chain function in the itertools module may be used to obtain the elements from a list. After that, make an empty set and start adding elements to it one by one. The functions used in this approach are map(), chain.from_iterable(), set(), loop.

Algorithm

  1. Import Chain from Itertools
  2. Initializing List
  3. Print the original List
  4. Extract the digits using map() + chain.from_iterable() + set() + loop
  5. Print the result

Program To Extract Digit from Tuples List

In this program, we utilized the map(), chain.from iterable(), and set() methods. The elements are processed using lambda and chain. from iterable(), and the set() method removes duplicate digits and outputs the tuple's unique digits.

from itertools import chain
List = [(25, 13), (13, 19), (11, 10), (99, 12)]
# list
print("Orignal list : ", List)
temp = map(lambda a: str(a), chain.from_iterable(List))
#  set and add
result = set()
for i in temp:
   for digit in i:
      result.add(digit)
# Result
print("set of digits: ",result)


Original list : [(25, 13), (13, 19), (11, 10), (99, 12)])
set of digits: set(['1', '0', '3', '2', '5', '9'])

Conclusion

In this tutorial, we have seen two approaches to extract the digits from the tuples list. The first approach is by using the regex expression method which means the needed unique digits are extracted using an appropriate regex expression and the second approach is by using chain and sets which uses function map(), chain.from_iterable(), set() and loop.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.