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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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().

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
Output list is : {‘8’, ‘4’, ‘0’, ‘2’, ‘1’, ‘3’}



My Personal Notes arrow_drop_up


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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.