ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

# 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}
```
ADVERTISEMENT
ADVERTISEMENT

Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.