Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert list of tuples into digits in Python
Python has a wide variety of data manipulation capabilities. We have a scenario in which we are given a list which has elements which are pair of numbers as tuples. In this article we will see how to extract the unique digits from the elements of a list which are tuples.
Using Regular Expression and Set
We can use regular expression module and its function called sub. It is used to replace a string that matches a regular expression instead of perfect match. So we design a regular expression to convert the tuples into normal strings and then apply the set function to get the unique digits ?
import re
listA = [(21, 3), (13, 4), (15, 7), (8, 11)]
# Given list
print("Given list : \n", listA)
# Remove all brackets, parentheses, commas, and spaces
temp = re.sub(r'[\[\]\(\), ]', '', str(listA))
# Using set to get unique digits
res = [int(i) for i in set(temp)]
# Result
print("List of digits: \n", res)
The output of the above code is ?
Given list : [(21, 3), (13, 4), (15, 7), (8, 11)] List of digits: [1, 2, 3, 4, 5, 7, 8]
Using itertools.chain and Set
The itertools module provides chain method which we can use to get the elements from the list. Then create an empty set and keep adding the elements to that set one by one ?
from itertools import chain
listA = [(21, 3), (13, 4), (15, 7), (8, 11)]
# Given list
print("Given list : \n", listA)
# Chain all numbers and convert to strings
temp = map(lambda x: str(x), chain.from_iterable(listA))
# Using set to store unique digits
res = set()
for i in temp:
for elem in i:
res.add(elem)
# Convert set to list for display
result_list = list(res)
# Result
print("List of digits: \n", result_list)
The output of the above code is ?
Given list : [(21, 3), (13, 4), (15, 7), (8, 11)] List of digits: ['1', '2', '3', '4', '5', '7', '8']
Using String Concatenation
A simpler approach is to concatenate all numbers as strings and then extract unique digits directly ?
listA = [(21, 3), (13, 4), (15, 7), (8, 11)]
# Given list
print("Given list : \n", listA)
# Concatenate all numbers as a single string
all_digits = ''.join(str(num) for tuple_pair in listA for num in tuple_pair)
# Get unique digits and convert to integers
unique_digits = [int(digit) for digit in set(all_digits)]
# Result
print("List of digits: \n", unique_digits)
The output of the above code is ?
Given list : [(21, 3), (13, 4), (15, 7), (8, 11)] List of digits: [1, 2, 3, 4, 5, 7, 8]
Comparison
| Method | Complexity | Best For |
|---|---|---|
| Regular Expression | Medium | Complex string patterns |
| itertools.chain | High | Large datasets |
| String Concatenation | Low | Simple and readable code |
Conclusion
The string concatenation method is the most straightforward approach for extracting unique digits from a list of tuples. Use regular expressions for complex patterns or itertools.chain for memory−efficient processing of large datasets.
