Python | Convert list of tuples into digits
Given a list of tuples, the task is to convert it into list of all digits which exists in elements of list.
Let’s discuss certain ways in which this task is performed.
Method #1: Using re
The most concise and readable way to convert list of tuple into list of all digits which exists in elements of list is by using re.
# Python code to convert list of tuples into # list of all digits which exists # in elements of list. # Importing import re # Input list initialization lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)] # Using re temp = re.sub(r'[\[\]\(\), ]', '', str(lst)) # Using set Output = [int(i) for i in set(temp)] # Printing output print("Initial List is :", lst) print("Output list is :", Output) |
Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
Output list is : [1, 4, 8, 0, 3, 2]
Method #2: Using itertools.chain() and lambda()
This is yet another way to perform this particular task using lambda().
# Python code to convert list of tuples into # list of all digits which exists # in elements of list. # Importing from itertools import chain # Input list initialization lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)] # using lambda temp = map(lambda x: str(x), chain.from_iterable(lst)) # Output list initialization Output = set() # Adding element in Output for x in temp: for elem in x: Output.add(elem) # Printing output print("Initial List is :", lst) print("Output list is :", Output) |
Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
Output list is : {‘8’, ‘4’, ‘0’, ‘2’, ‘1’, ‘3’}
Recommended Posts:
- Python | Convert string tuples to list tuples
- Python | Convert list of tuples to list of strings
- Python | Convert list of strings to list of tuples
- Python | Convert String to list of tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert dictionary to list of tuples
- Python | Convert list elements to bi-tuples
- Python | Convert list of tuples to dictionary value lists
- Python | Convert list of tuples into list
- Python | Convert list of tuples to list of list
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Count tuples occurrence in list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



