Python Program to remove a specific digit from every element of the list
Last Updated :
22 Mar, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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.
OutputModified 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))
OutputThe 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.
Similar Reads
Python Program to Removes Every Element From A String List Except For a Specified letter
Given a List that contains only string elements, the following program shows methods of how every other alphabet can be removed from elements except for a specific one and then returns the output. Input : test_list = ["google", "is", "good", "goggled", "god"], K = 'g' Output : ['gg', '', 'g', 'ggg',
4 min read
Python program to extract only the numbers from a list which have some specific digits
Given the elements List, extract numbers with specific digits. Input : test_list = [3456, 23, 128, 235, 982], dig_list = [2, 3, 5, 4] Output : [23, 235] Explanation : 2, 3 and 2, 3, 5 are in digit list, hence extracted elements.Input : test_list = [3456, 23, 28, 235, 982], dig_list = [2, 3, 5, 4, 8]
7 min read
Python | Remove element from given list containing specific digits
Given a list, the task is to remove all those elements from list which contains the specific digits. Examples: Input: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16] no_delete = ['2', '3', '4', '0'] Output: [1, 5, 6, 7, 8, 9, 11, 15, 16] Explanation: Numbers 2, 3, 4, 10, 12, 13, 14 c
9 min read
Python Program to Remove Palindromic Elements from a List
Given a list, the task here is to write Python programs that can remove all the elements which have palindromic equivalent element present in the list. Examples: Input : test_list = [54, 67, 12, 45, 98, 76, 9] Output : [12, 98] Explanation : 67 has 76 as palindromic element, both are omitted. Input
2 min read
Python Program to Remove First Diagonal Elements from a Square Matrix
Given a square matrix of N*N dimension, the task is to write a Python program to remove the first diagonal. Examples: Input : test_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Output : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4,
6 min read
Python Program to Split the Even and Odd elements into two different lists
In Python, it's a common task to separate even and odd numbers from a given list into two different lists. This problem can be solved using various methods. In this article, weâll explore the most efficient ways to split even and odd elements into two separate lists.Using List ComprehensionList comp
3 min read
Python Program to Delete Specific Line from File
In this article, we are going to see how to delete the specific lines from a file using PythonThroughout this program, as an example, we will use a text file named months.txt on which various deletion operations would be performed.Method 1: Deleting a line using a specific positionIn this method, th
3 min read
Python program to remove last N characters from a string
In this article, weâll explore different ways to remove the last N characters from a string in Python. This common string manipulation task can be achieved using slicing, loops, or built-in methods for efficient and flexible solutions.Using String SlicingString slicing is one of the simplest and mos
2 min read
Python Program to extracts elements from the list with digits in increasing order
Given a List of elements, extract all elements which have digits that are increasing in order. Input : test_list = [1234, 7373, 3643, 3527, 148, 49] Output : [1234, 148, 49] Explanation : All elements have increasing digits.Input : test_list = [12341, 7373, 3643, 3527, 1481, 491] Output : [] Explana
5 min read
Python program to replace first 'K' elements by 'N'
Given a List, replace first K elements by N. Input : test_list = [3, 4, 6, 8, 4, 2, 6, 9], K = 4, N = 3 Output : [3, 3, 3, 3, 4, 2, 6, 9] Explanation : First 4 elements are replaced by 3. Input : test_list = [3, 4, 6, 8, 4, 2, 6, 9], K = 2, N = 10 Output : [10, 10, 6, 8, 4, 2, 6, 9] Explanation : Fi
5 min read