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
test_list = [ 345 , 893 , 1948 , 34 , 2346 ]
print ( "The original list is : " + str (test_list))
K = 3
res = []
for ele in test_list:
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])))
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
test_list = [ 345 , 893 , 1948 , 34 , 2346 ]
print ( "The original list is : " + str (test_list))
K = 3
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]
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
test_list = [ 345 , 893 , 1948 , 34 , 2346 ]
print ( "The original list is : " + str (test_list))
K = 3
res = []
for ele in test_list:
x = str (ele).replace( str (K), '')
res.append( int (x))
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
test_list = [ 345 , 893 , 1948 , 34 , 2346 ]
print ( "The original list is : " + str (test_list))
K = 3
res = list ( map ( lambda x: str (x).replace( str (K), ''), test_list))
res = list ( map ( int , res))
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
test_list = [ 345 , 893 , 1948 , 34 , 2346 ]
print ( "The original list is : " + str (test_list))
K = 3
res = []
for i in test_list:
x = str (i)
y = x.split( str (K))
y = "".join(y)
res.append( int (y))
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
last_digit = number % 10
remaining_number = remove_digit(number / / 10 , digit)
if last_digit = = digit:
return remaining_number
else :
return remaining_number * 10 + last_digit
test_list = [ 345 , 893 , 1948 , 34 , 2346 ]
K = 3
res = [remove_digit(ele, K) for ele in test_list]
print ( "Modified List : " + str (res))
|
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
test_list = [ 345 , 893 , 1948 , 34 , 2346 ]
print ( "The original list is : " + str (test_list))
K = 3
str_list = [ str (i) for i in test_list]
modified_str_list = [s.replace( str (K), '') for s in str_list]
modified_list = [ int (s) for s in modified_str_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.
Similar Reads
Python program to remove last element from set
Given a set, the task is to write a Python program to delete the last element from the set. Example: Input: {1,2,3,4}Remove 4Output: {1,2,3} Input: {"Geeks","For"}Remove GeeksOutput: {"For"}Explanation: The idea is to remove the last element from the set 4 in the first case and "Geeks" in the second
2 min read
Python program to Remove the last element from list
Given a list of numbers or strings, the task is to write a Python program to remove the last element from a list. Example: Input : [12,53,11,76,22,21] Output : [12,53,11,76,22] Input : ["abc","gfg","text","python"] Output : ["abc","gfg","text"] The application of this type of problem in day-day prog
3 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 con
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 Last character from the string
Given a string, the task is to write a Python program to remove the last character from the given string. Example: Input: "GeeksForGeeks"Output: "GeeksForGeek" Input: "1234"Output: "123"Explanation: Here we are removing the last character of the original string. Note: Strings are immutable in Python
3 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
Remove elements larger than a specific value from a list in Python
In this article, we will learn to remove elements from a list that is larger than a specific value in Python. Example Input: [12, 33, 10, 20, 25], value = 21 Output: [12, 10, 20] Explanation: Removed all element from the list that are greater than 21. Remove list elements greater than a given value
4 min read
Python program to remove last element from Tuple
Given a tuple, the task is to write a Python program to delete the last element in the tuple in Python. Example: Input: ("geeks", "for", "geeks") Output:("geeks", "for") Explanation: Here we are deleting the last element of the tuple and finally modifying the original one. Note: Tuples are immutable
3 min read
Python program to remove duplicate elements index from other list
Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1
7 min read