Open In App

Python | Remove element from given list containing specific digits

Improve
Improve
Like Article
Like
Save
Share
Report

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 contains digits 
from no_delete, therefore remove them.

Input: lst = [1, 2, 3, 4, 5, 6, 7, 8, 13, 15, 16]
       no_delete = {'6', '5', '4', '3'}
Output: [1, 2, 7, 8, 9, 10, 11, 12]
Explanation:
Numbers 3, 4, 5, 6, 13, 14, 15, 16 contains digits 
from no_delete, therefore remove them.

  Below are some methods to do the task. 

Method #1: Using Iteration 

Python3




# Python code to remove all those elements
# from list which contains certain digits
 
# Input List Initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
 
# Numbers to delete
no_delete = [1, 0]
 
# Output List Initialisation
Output = []
 
# Using iteration to remove all the elements
for elem in Input:
    flag = 1
    temp = elem
    while elem > 0:
        rem = elem % 10
        elem = elem//10
        if rem in no_delete:
            flag = 0
    if flag == 1:
        Output.append(temp)
 
# Printing Output
print("Initial list is :", Input)
print("Delete list :", no_delete)
print("List after removing elements is :", Output)


Output:

Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
Delete list : [1, 0]
List after removing elements is : [2, 3, 4, 5, 6, 7, 8, 9]

  Method #2: Using List comprehension and any() function 

Python3




# Python code to remove all those elements from list
# which contains certain digits
 
# Input List Initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
 
# Numbers to delete
no_delete = ['2', '3', '4', '0']
 
# using list comprehension and any()
Output = [a for a in Input if not
          any(b in no_delete for b in str(a))]
 
# Printing Output
print("Initial list is :", Input)
print("Delete list :", no_delete)
print("List after removing elements is :", Output)


Output:

Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
Delete list : ['2', '3', '4', '0']
List after removing elements is : [1, 5, 6, 7, 8, 9, 11, 15, 16]

  Method #3: Using List comprehension and set() 

Python3




# Python code to remove all those elements from list
# which contains certain digits
 
# Input List Initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
 
# Numbers to delete
no_delete = {'6', '5', '4', '3'}
 
# Using list comprehension and set
Output = [x for x in Input
         if not no_delete & set(str(x))]
 
# Printing Output
print("Initial list is :", Input)
print("Delete list :", no_delete)
print("List after removing elements is :", Output)


Output:

Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
Delete list : {'3', '4', '6', '5'}
List after removing elements is : [1, 2, 7, 8, 9, 10, 11, 12]

Time complexity: O(n*n), where n is the length of the test_list. The List comprehension and set() takes O(n*n) time
Auxiliary Space: O(n), where n is the number of elements in the input list

Method #4: Using the filter function to remove elements

This method uses a lambda function and the all function to check if all digits in the element are not present in the set of digits to delete. The filtered list is then obtained using the filter function.

Here is an example of using the filter function to remove elements containing specific digits from a list:

Python3




# Define a set of digits to delete
no_delete = {'2', '3', '4', '0'}
 
# Initialize input list
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
 
# Filter out elements containing digits from the set of digits to delete
filtered_lst = list(filter(lambda x: all(digit not in no_delete for digit in str(x)), lst))
 
# Print the input and output lists
print("Initial list is :", lst)
print("Delete list :", no_delete)
print("List after removing elements is :", filtered_lst)
#This code is contributed by Edula Vinay Kumar Reddy


Output

Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
Delete list : {'0', '4', '2', '3'}
List after removing elements is : [1, 5, 6, 7, 8, 9, 11, 15, 16]

The time complexity of the filter approach is O(n*m), where n is the length of the input list.  m is number of no delete list.

The space complexity of the filter approach is also O(n), as the filtered list must be stored in memory. This is because the filter function returns a new list containing only the elements that meet the specified condition.

Method #5: Using operator.countOf() method

Python3




import operator as op
# Define a set of digits to delete
no_delete = {'2', '3', '4', '0'}
 
# Initialize input list
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
 
# Filter out elements containing digits from the set of digits to delete
filtered_lst = list(filter(lambda x: all(op.countOf(
    no_delete, digit) == 0 for digit in str(x)), lst))
 
# Print the input and output lists
print("Initial list is :", lst)
print("Delete list :", no_delete)
print("List after removing elements is :", filtered_lst)
# This code is contributed by vikkycirus


Output

Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
Delete list : {'3', '4', '0', '2'}
List after removing elements is : [1, 5, 6, 7, 8, 9, 11, 15, 16]

Time Complexity:O(N)

Auxiliary Space: O(N)

Method#6: Using regular expressions

Python3




import re
 
# Input List Initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
 
# Numbers to delete
no_delete = ['2', '3', '4', '0']
 
def check_delete(num):
    pattern = '|'.join(no_delete)
    return not bool(re.search(pattern, str(num)))
 
Output = list(filter(check_delete, Input))
 
# Printing Output
print("Initial list is :", Input)
print("Delete list :", no_delete)
print("List after removing elements is :", Output)
#This code contributed Vinay Pinjala.


Output

Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
Delete list : ['2', '3', '4', '0']
List after removing elements is : [1, 5, 6, 7, 8, 9, 11, 15, 16]

Time Complexity:O(N)

Auxiliary Space: O(N)

Method#7: Using Recursive method.

Algorithm:

  1. Define a function remove_digits that takes two arguments, input_list and no_delete.
  2. If the input_list is empty, return an empty list.
  3. Check if the first element of the input_list contains any digits to delete using a while loop and a flag variable.
  4. If the flag is still set to 1, it means the first element can be kept in the output list, so append it to the result of calling remove_digits recursively with the rest of the input_list.
  5. If the flag is set to 0, skip the first element and call remove_digits recursively with the rest of the input_list.
  6. Return the final output list.
  7. Call the remove_digits function with an example input and print the output.
     

Python3




def remove_digits(input_list, no_delete):
    # base case: empty input list
    if not input_list:
        return []
     
    # recursive case: remove the first element if it doesn't contain any digits to delete
    elem = input_list[0]
    flag = 1
    temp = elem
    while elem > 0:
        rem = elem % 10
        elem = elem // 10
        if rem in no_delete:
            flag = 0
    if flag == 1:
        return [temp] + remove_digits(input_list[1:], no_delete)
    else:
        return remove_digits(input_list[1:], no_delete)
     
# Input List Initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
# Numbers to delete
no_delete = [1, 0]
Output = remove_digits(Input, no_delete)
# Printing Output
print("Initial list is:", Input)
print("Digits to delete are:", no_delete)
print("List after removing elements is:", Output)
#This code contributed by tvsk.


Output

Initial list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
Digits to delete are: [1, 0]
List after removing elements is: [2, 3, 4, 5, 6, 7, 8, 9]

The time complexity of this solution is O(nm), where n is the length of the input list and m is the maximum number of digits in an element of the input list. This is because we iterate over each element of the input list and each digit in each element. 

The space complexity is also O(n), since we store the output list and call the remove_digits function recursively for each element of the input list.

Approch using Numpy:

In this approach, we first convert the input list into a NumPy array. We then use a NumPy function called logical_not to obtain the logical NOT of the boolean array generated by the list comprehension that checks if any of the elements in the input list contain the specified digits. We use the set function to convert the element to a set of characters and check if there is any overlap between the set and the no_delete set using the & operator. Finally, we index the input array using the logical NOT of the boolean array to obtain the output.

Python3




import numpy as np
 
# Input List Initialization
Input = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16])
 
# Numbers to delete
no_delete = {'2', '0', '4', '3'}
 
# Using NumPy to remove all the elements
Output = Input[np.logical_not(
    [bool(set(str(elem)) & no_delete) for elem in Input])]
 
# Printing Output
print("Initial list is :", Input)
print("Delete list :", no_delete)
print("List after removing elements is :", Output)


Output:

Initial list is : [ 1  2  3  4  5  6  7  8  9 10 11 12 14 13 15 16]
Delete list : {'3', '0', '2', '4'}
List after removing elements is : [ 1  5  6  7  8  9 11 15 16]

Time Complexity:
The time complexity of this approach is O(n), where n is the length of the input list. This is because we iterate through the input list only once.

Space Complexity:
The space complexity of this approach is O(n), where n is the length of the input list. This is because we create a NumPy array of size n and a boolean array of size n to store the output.



Last Updated : 05 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads