Python program to extract only the numbers from a list which have some specific digits
Last Updated :
06 Apr, 2023
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]
Output : [23, 28, 235]
Explanation : 2, 3; 2, 8 and 2, 3, 5 are in digit list, hence extracted elements.
Method #1 : Using list comprehension + all()
In this, we check for each element in number against the elements from target list to be present, if all are found in list, element is returned.
Python3
# Python3 code to demonstrate working of
# Elements with specific digits
# Using list comprehension + all()
# initializing list
test_list = [345, 23, 128, 235, 982]
# printing original list
print("The original list is : " + str(test_list))
# initializing digit list
dig_list = [2, 3, 5, 4]
# checking for all digits using all()
res = [sub for sub in test_list if all(
int(ele) in dig_list for ele in str(sub))]
# printing result
print("Extracted elements : " + str(res))
OutputThe original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using filter() + lambda + all()
In this, filtering of elements is done using filter() + lambda, all() is used to check for all the digits from other list.
Python3
# Python3 code to demonstrate working of
# Elements with specific digits
# Using filter() + lambda + all()
# initializing list
test_list = [345, 23, 128, 235, 982]
# printing original list
print("The original list is : " + str(test_list))
# initializing digit list
dig_list = [2, 3, 5, 4]
# filter() used to filter from logic
res = list(filter(lambda sub: all(
int(ele) in dig_list for ele in str(sub)), test_list))
# printing result
print("Extracted elements : " + str(res))
OutputThe original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in filter() + lambda + all() function which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of the input list.
Method #3 : Using Counter() + keys()+ all()
Python3
# Python3 code to demonstrate working of
# Elements with specific digits
from collections import Counter
# initializing list
test_list = [345, 23, 128, 235, 982]
# printing original list
print("The original list is : " + str(test_list))
# initializing digit list
dig_list = [2, 3, 5, 4]
freq = Counter(dig_list)
# checking for all digits using all() and keys() function
res = [sub for sub in test_list if all(
int(ele) in freq.keys() for ele in str(sub))]
# printing result
print("Extracted elements : " + str(res))
OutputThe original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time Complexity: O(n) as we used hashing for searching whether the digit is present in dig_list or not
Method #4: Using itertools.filterfalse() method
Python3
# Python3 code to demonstrate working of
# Elements with specific digits
import itertools
# initializing list
test_list = [345, 23, 128, 235, 982]
# printing original list
print("The original list is : " + str(test_list))
# initializing digit list
dig_list = [2, 3, 5, 4]
# filter() used to filter from logic
res = list(itertools.filterfalse(lambda sub : not all(int(ele) in dig_list for ele in str(sub)), test_list))
# printing result
print("Extracted elements : " + str(res))
OutputThe original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time Complexity:O(N)
Auxiliary Space:O(N)
Method #5:Using recursion
Algorithm:
- Define a function get_elements_with_specific_digits() that takes a list test_list and a digit list dig_list as input.
- Check if the input list test_list is empty. If it is, return an empty list.
- If the input list test_list is not empty, take the first element sub of the list.
- Check if all of the digits in sub are in the digit list dig_list.
- If all of the digits in sub are in dig_list, add sub to the result list and call the function recursively on the remaining elements of test_list (i.e., the elements starting from the second element).
- If not all of the digits in sub are in dig_list, call the function recursively on the remaining elements of test_list (i.e., the elements starting from the second element).
- Return the result list.
Python3
# Python3 code to demonstrate working of
# Elements with specific digits
# Using recursion
def get_elements_with_specific_digits(test_list, dig_list):
if not test_list:
return []
sub = test_list[0]
if all(int(ele) in dig_list for ele in str(sub)):
return [sub] + get_elements_with_specific_digits(test_list[1:], dig_list)
else:
return get_elements_with_specific_digits(test_list[1:], dig_list)
# initializing list
test_list = [345, 23, 128, 235, 982]
# printing original list
print("The original list is : " + str(test_list))
# initializing digit list
dig_list = [2, 3, 5, 4]
# call the function to get the filtered list
res = get_elements_with_specific_digits(test_list, dig_list)
# printing result
print("Extracted elements : " + str(res))
#This code is contributed by Jyothi Pinjala.
OutputThe original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of an integer in the list (i.e., the number of digits in the largest integer). This is because we iterate through the entire list and for each integer in the list, we check if each of its digits is in the digit list.
Auxiliary space: O(n), where n is the length of the input list. This is because we create a new list to store the filtered elements.
Method 6: Use a for a loop
Approach:
- Convert the digit list into a set for O(1) lookup time.
- Initialize an empty list to store the filtered elements.
- Iterate through each element of the test_list using a for loop.
- Convert the current element to a string and convert each character to an integer using map() function.
- Check if the set of digits in the digit list is a subset of the set of digits in the current element.
- If it is, append the current element to the result list.
- Return the result list.
- Here is the code for the above approach with time and auxiliary space complexity:
Python3
def get_elements_with_specific_digits(test_list, dig_list):
# Convert digit list to set
dig_set = set(dig_list)
# Initialize empty list for filtered elements
res = []
# Iterate through each element in test_list
for ele in test_list:
# Convert element to string and map each character to int
digits = set(map(int, str(ele)))
# Check if digit set is subset of dig_set
if digits.issubset(dig_set):
# Append current element to result list
res.append(ele)
# Return filtered list
return res
# initializing list
test_list = [345, 23, 128, 235, 982]
# printing original list
print("The original list is : " + str(test_list))
# initializing digit list
dig_list = [2, 3, 5, 4]
# call the function to get the filtered list
res = get_elements_with_specific_digits(test_list, dig_list)
# printing result
print("Extracted elements : " + str(res))
OutputThe original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time complexity: O(n * k), where n is the length of the test_list and k is the maximum number of digits in any element of the test_list.
Auxiliary space: O(n), where n is the length of the test_list.
Similar Reads
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 occurrenc
7 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 find tuples which have all elements divisible by K from a list of tuples
Given a list of tuples. The task is to extract all tuples which have all elements divisible by K. Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6 Output : [(6, 24, 12), (60, 12, 6)] Explanation : Both tuples have all elements multiple of 6. Input : test_list = [(6, 24, 12), (60,
7 min read
Python Program to test if the String only Numbers and Alphabets
Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Expl
4 min read
Python program to extract numeric suffix from string
Given a string of characters with digits embedded in it. The task is to write a Python program to extract all the numbers that are trailing, i.e at the suffix of the string. Examples:Input : test_str = "GFG04"Output : 04Explanation : 04 is suffix of string and number hence extracted.Input : test_str
7 min read
Python program to find the smallest number in a file
Given a text file, write a Python program to find the smallest number in the given text file.Examples:Input: gfg.txtOutput: 9Explanation: Contents of gfg.txt: I live at 624 Hyderabad.My mobile number is 52367. My favourite number is 9.Numbers present in the text file are 9,624,52367Minimum number is
3 min read
Python program to calculate the number of digits and letters in a string
In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Python - Sort list of numbers by sum of their digits
Sorting a list of numbers by the sum of their digits involves ordering the numbers based on the sum of each individual digit within the number. This approach helps prioritize numbers with smaller or larger digit sums, depending on the use case.Using sorted() with a Lambda Functionsorted() function w
2 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 move numbers to the end of the string
Given a string, the task is to write a Python program to move all the numbers in it to its end. Examples: Input : test_str = 'geek2eeks4g1eek5sbest6forall9' Output : geekeeksgeeksbestforall241569 Explanation : All numbers are moved to end. Input : test_str = 'geekeeksg1eek5sbest6forall9' Output : ge
6 min read