 GeeksforGeeks App
Open App Browser
Continue

# 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

## Python3

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

Output

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

Method #2: Using itertools.chain() and lambda() This is yet another way to perform this particular task using lambda().

## Python3

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

Output

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

Method #3: Using list(),map(),join() and set() methods

Initially we convert list containing tuple of integers to list containing tuple of strings.Later we will concatenate the string(obtained by joining tuple strings) to an empty string.And then use set to remove duplicates.Finally convert them to integer type and print output list

## Python3

 `# Python code to convert list of tuples into``# list of all digits which exists``# in elements of list.`  `# Input list initialization``lst ``=` `[(``11``, ``100``), (``22``, ``200``), (``33``, ``300``), (``44``, ``400``), (``88``, ``800``)]` `p``=``""``for` `i ``in` `lst:``    ``x``=``list``(``map``(``str``,i))``    ``p``+``=``"".join(x)``p``=``list``(``map``(``int``,``set``(p)))` `# Printing output``print``(``"Initial List is :"``, lst)``print``(``"Output list is :"``, p)`

Output

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

Time complexity: O(n), where n is the length of list
Auxiliary Space: O(m), where m is the number of digits.

Method #4: Using list comprehension and the isdigit method

You can use a list comprehension and the isdigit method to extract all the digits from the tuples in the list. The isdigit method returns True if a character is a digit, and False otherwise.

## Python3

 `# Initialize the list``lst ``=` `[(``11``, ``100``), (``22``, ``200``), (``33``, ``300``), (``44``, ``400``), (``88``, ``800``)]` `# Extract the digits using a list comprehension and the isdigit method``digits ``=` `set``()` `# Print the resulting list``print``(``"The list of digits:"``, digits)``#This code is contributed by Edula Vinay Kumar Reddy`

Output

`The list of digits: {'0', '8', '1', '2', '3', '4'}`

Time complexity is O(n)
Auxiliary Space is O(m), where n is the length of the list and m is the number of digits.

Method #5: Using nested loops and string conversion

Step-by-step approach:

• Initialize the input list lst with the given values.
• Initialize an empty list digits to store the extracted digits.
• Loop through each tuple tup in the input list.
• Loop through each item in the current tuple.
• Loop through each character char in the string representation of the current item.
• Check if the current character is a digit using the isdigit() method.
• If it is a digit, convert it to an integer using the int() function and append it to the digits list.
• Use the set() function to remove duplicates from the digits list and convert it back to a list.
• Print the initial list and the output list.

Below is the implementation of the above approach:

## Python3

 `# Input list initialization``lst ``=` `[(``11``, ``100``), (``22``, ``200``), (``33``, ``300``), (``44``, ``400``), (``88``, ``800``)]` `# Using nested loops and string conversion``digits ``=` `[]``for` `tup ``in` `lst:``    ``for` `item ``in` `tup:``        ``for` `char ``in` `str``(item):``            ``if` `char.isdigit():``                ``digits.append(``int``(char))` `# Using set to remove duplicates``Output ``=` `list``(``set``(digits))` `# Printing output``print``(``"Initial List is :"``, lst)``print``(``"Output list is :"``, Output)`

Output

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

Time complexity: O(n*m), where n is the length of the input list and m is the maximum length of the items in the tuples (assuming integer items only).
Auxiliary space: O(k), where k is the total number of digits in all the items in the input list.

Method #6: Using numpy.ravel():

• Import the numpy library.
• Initialize a list of tuples called last.
• Use numpy’s ravel() method to convert the list of tuples to a 1-D numpy array called arr.
• Use a list comprehension and the isdigit() method to extract the digits from the array and convert them to integers.
• Use set() to remove any duplicates in the list of digits.
• Convert the set back to a list called digits.
• Print the original list and the resulting list.

## Python3

 `import` `numpy as np` `# Initialize the list``lst ``=` `[(``11``, ``100``), (``22``, ``200``), (``33``, ``300``), (``44``, ``400``), (``88``, ``800``)]` `# Convert the list of tuples to a 1-D numpy array using ravel() method``arr ``=` `np.array(lst).ravel()` `# Extract the digits using a list comprehension and the isdigit method``digits ``=` `[``int``(d) ``for` `d ``in` `''.join(``map``(``str``, arr)) ``if` `d.isdigit()]` `# Remove duplicates using set``digits ``=` `list``(``set``(digits))` `# Print the resulting list``print``(``"Initial List is :"``, lst)``print``(``"Output list is :"``, digits)`

Output

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

The time complexity of this code is O(n), where n is the total number of digits.

The space complexity of this code is O(n), where n is the total number of digits.

My Personal Notes arrow_drop_up