GeeksforGeeks App
Open App
Browser
Continue

Python Program to remove a specific digit from every element of the list

Given a list of elements, the task here is to write a Python program that can remove the presence of all a specific digit from every element and then return the resultant list.

Examples:

Input : test_list = [333, 893, 1948, 34, 2346], K = 3
Output : [”, 89, 1948, 4, 246]
Explanation : All occurrences of 3 are removed.

Input : test_list = [345, 893, 1948, 34, 2346], K = 5
Output : [34, 893, 1948, 34, 2346]
Explanation : All occurrences of 5 are removed.

Method 1 : Using loop, str() and join()

In this, we perform the task of reforming elements by converting them to strings and checking for each digit, and ignoring while joining to get new element. Lastly, each element is converted to integer using int().

Example:

Python3

 `# initializing list``test_list ``=` `[``345``, ``893``, ``1948``, ``34``, ``2346``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `3` `res ``=` `[]``for` `ele ``in` `test_list:` `    ``# joining using join(),``    ``if` `list``(``set``(``str``(ele)))[``0``] ``=``=` `str``(K) ``and` `len``(``set``(``str``(ele))) ``=``=` `1``:``        ``res.append('')``    ``else``:``        ``res.append(``int``(''.join([el ``for` `el ``in` `str``(ele) ``if` `int``(el) !``=` `K])))` `# printing result``print``(``"Modified List : "` `+` `str``(res))`

Output

```The original list is : [345, 893, 1948, 34, 2346]
Modified List : [45, 89, 1948, 4, 246]```

Time complexity: O(n*m), where n is the length of the list “test_list”, and m is the average length of the string representation of the elements in “test_list”.
Auxiliary Space: O(n), where n is the length of the list “res”.

Method 2 : Using list comprehension, int(), str() and join()

Similar to the above method, joining is done using join() and interconversion is performed using int() and str().

Python3

 `# initializing list``test_list ``=` `[``345``, ``893``, ``1948``, ``34``, ``2346``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `3` `# list comprehension performing task as one liner``res ``=` `['' ``if` `list``(``set``(``str``(ele)))[``0``] ``=``=` `str``(K) ``and` `len``(``set``(``str``(ele))) ``=``=` `1` `else` `int``(``    ``''.join([el ``for` `el ``in` `str``(ele) ``if` `int``(el) !``=` `K])) ``for` `ele ``in` `test_list]` `# printing result``print``(``"Modified List : "` `+` `str``(res))`

Output

```The original list is : [345, 893, 1948, 34, 2346]
Modified List : [45, 89, 1948, 4, 246]```

Time Complexity: O(N*N), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(N), where n is the number of elements in the list “test_list”.

Method 3: Using replace() method

Python3

 `# initializing list``test_list ``=` `[``345``, ``893``, ``1948``, ``34``, ``2346``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `3` `# removing specific digit``res ``=` `[]``for` `ele ``in` `test_list:``    ``x ``=` `str``(ele).replace(``str``(K), '')``    ``res.append(``int``(x))` `# printing result``print``(``"Modified List : "` `+` `str``(res))`

Output

```The original list is : [345, 893, 1948, 34, 2346]
Modified List : [45, 89, 1948, 4, 246]```

Time complexity: O(nm), where n is the length of the list and m is the length of the largest integer in the list.
Auxiliary space: O(n), since we are creating a new list to store the modified integers.

Method #4: Using map(),lambda functions.

Python3

 `# initializing list``test_list ``=` `[``345``, ``893``, ``1948``, ``34``, ``2346``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `3` `# removing specific digit``res ``=` `list``(``map``(``lambda` `x: ``str``(x).replace(``str``(K), ''), test_list))``res ``=` `list``(``map``(``int``, res))``# printing result``print``(``"Modified List : "` `+` `str``(res))`

Output

```The original list is : [345, 893, 1948, 34, 2346]
Modified List : [45, 89, 1948, 4, 246]```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5 : Using split(),join() methods

Python3

 `# initializing list``test_list ``=` `[``345``, ``893``, ``1948``, ``34``, ``2346``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `3` `# list comprehension performing task as one liner``res ``=` `[]``for` `i ``in` `test_list:``    ``x ``=` `str``(i)``    ``y ``=` `x.split(``str``(K))``    ``y ``=` `"".join(y)``    ``res.append(``int``(y))` `# printing result``print``(``"Modified List : "` `+` `str``(res))`

Output

```The original list is : [345, 893, 1948, 34, 2346]
Modified List : [45, 89, 1948, 4, 246]```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method#6: Using Recursion

This code takes a number and a target digit as arguments and removes all occurrences of the target digit from the number using recursion. The function first extracts the right-most digit of the number, removes it, and then checks the remaining number recursively. If the right-most digit matches the target digit, it is skipped; otherwise, it is added back to the remaining number. Finally, the function returns the modified number.

Python3

 `def` `remove_digit(number, digit):``    ``if` `number ``=``=` `0``:``        ``return` `0` `    ``# extract the right-most digit``    ``last_digit ``=` `number ``%` `10` `    ``# remove the right-most digit and check recursively for the remaining number``    ``remaining_number ``=` `remove_digit(number ``/``/` `10``, digit)` `    ``# check if the last digit matches the target digit, and return the appropriate value``    ``if` `last_digit ``=``=` `digit:``        ``return` `remaining_number``    ``else``:``        ``return` `remaining_number ``*` `10` `+` `last_digit`  `# example usage``test_list ``=` `[``345``, ``893``, ``1948``, ``34``, ``2346``]``K ``=` `3``res ``=` `[remove_digit(ele, K) ``for` `ele ``in` `test_list]``print``(``"Modified List : "` `+` `str``(res))` `# This code is contributed by Vinay Pinjala.`

Output

`Modified List : [45, 89, 1948, 4, 246]`

Time complexity: O(N)
Where n is the number of elements in the list. This is because we need to iterate through each element in the list and call the function recursively for each sublist.

Auxiliary Space: O(n)
Where n is the number of elements in the list. This is because we need to create a new list to store the modified elements, and each recursive call creates a new stack frame to store the state of the function.

Method #7: Using bitwise operations

Step-by-step approach:

• Define a function remove_digit_k that takes two arguments: a list lst and an integer k.
• Inside the function, define an empty list res to store the modified elements.
• Define a constant digit_mask as the bitwise complement of a number with a single 1-bit in the k-th position (i.e., digit_mask = ~(1 << k)).
• Loop through each element ele in the input list lst.
• If the element ele is an integer and the k-th digit is not present in its decimal representation (i.e., (ele >> k) & 1 == 0), append it to the res list as-is.
• Otherwise, use the bitwise AND operator to clear the k-th bit from the integer ele (i.e., cleared = ele & digit_mask), then convert it to a string and append it to the res list as an integer (i.e., res.append(int(str(cleared)))).
• Return the res list.

Method 7: Using string manipulation and list comprehension

In this approach, we convert the list of integers to a list of strings and then use string manipulation to remove the specific digit (K) from each string. Finally, we convert the list of strings back to a list of integers.

Step-by-step approach:

• Initialize the list of integers.
• Print the original list.
• Initialize K.
• Convert the list of integers to a list of strings using list comprehension.
• Use string manipulation to remove the specific digit (K) from each string using another list comprehension.
• Convert the list of modified strings back to a list of integers using list comprehension.
• Print the modified list.

Python3

 `# initializing list``test_list ``=` `[``345``, ``893``, ``1948``, ``34``, ``2346``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `3` `# converting the list of integers to a list of strings``str_list ``=` `[``str``(i) ``for` `i ``in` `test_list]` `# using string manipulation to remove the specific digit (K) from each string``modified_str_list ``=` `[s.replace(``str``(K), '') ``for` `s ``in` `str_list]` `# converting the list of modified strings back to a list of integers``modified_list ``=` `[``int``(s) ``for` `s ``in` `modified_str_list]` `# printing the modified list``print``(``"Modified List : "` `+` `str``(modified_list))`

Output

```The original list is : [345, 893, 1948, 34, 2346]
Modified List : [45, 89, 1948, 4, 246]```

Time complexity: O(n*k), where n is the length of the list and k is the length of the maximum number in the list.

Auxiliary space: O(n*k), as we are creating new lists of strings and integers.

My Personal Notes arrow_drop_up