Python – List Elements with given digit
Last Updated :
10 May, 2023
Given list of elements and a digit K, extract all the numbers which contain K digit.
Input : test_list = [56, 72, 875, 9, 173], K = 5
Output : [56, 875]
Explanation : 56 and 875 has “5” as digit, hence extracted.
Input : test_list = [56, 72, 875, 9, 173], K = 4
Output : []
Explanation : No number has 4 as digit.
Method #1 : Using list comprehension + str()
This is one of the ways in which this task can be performed. In this, we convert digit and element to string and then check if its inside that element. The element iteration is done inside list comprehension to get one-liner solution.
Python3
test_list = [ 56 , 72 , 875 , 9 , 173 ]
print ( "The original list is : " + str (test_list))
K = 7
res = [ele for ele in test_list if str (K) in str (ele)]
print ( "Elements with digit K : " + str (res))
|
Output
The original list is : [56, 72, 875, 9, 173]
Elements with digit K : [72, 875, 173]
Time Complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), as a new list “res” is created to store the elements with digit K.
Method #2 : Using filter() + lambda + str()
This is yet another way to solve this problem. In this, we use filter() + lambda along with str() to check conditionals and extract required elements.
Python3
test_list = [ 56 , 72 , 875 , 9 , 173 ]
print ( "The original list is : " + str (test_list))
K = 7
res = list ( filter ( lambda ele: str (K) in str (ele), test_list))
print ( "Elements with digit K : " + str (res))
|
Output
The original list is : [56, 72, 875, 9, 173]
Elements with digit K : [72, 875, 173]
Time complexity: O(n*n), where n is the length of the test_list. The filter() + lambda + str() takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required
Method #3 : Using find() method
Python3
test_list = [ 56 , 72 , 875 , 9 , 173 ]
print ( "The original list is : " + str (test_list))
K = 7
res = []
for i in test_list:
if ( str (i).find( str (K))! = - 1 ):
res.append(i)
print ( "Elements with digit K : " + str (res))
|
Output
The original list is : [56, 72, 875, 9, 173]
Elements with digit K : [72, 875, 173]
Method #4 : Using itertools.compress method:
Python3
import itertools
test_list = [ 56 , 72 , 875 , 9 , 173 ]
print ( "The original list is:" , test_list)
K = 7
res = list (itertools.compress(test_list, [ str (K) in str (x) for x in test_list]))
print ( "Elements with digit K:" , res)
|
Output
The original list is: [56, 72, 875, 9, 173]
Elements with digit K: [72, 875, 173]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using str()+replace() methods
Python3
test_list = [ 56 , 72 , 875 , 9 , 173 ]
print ( "The original list is : " + str (test_list))
K = 7
res = []
for i in test_list:
x = str (i)
y = str (K)
z = x.replace(y,"")
if (x! = z):
res.append(i)
print ( "Elements with digit K : " + str (res))
|
Output
The original list is : [56, 72, 875, 9, 173]
Elements with digit K : [72, 875, 173]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using map() + str() + filter() method:
- Convert all the elements in the test_list to string using map() function and str() method.
- Use the filter() function with lambda expression to filter out the elements from the list that do not contain the digit K.
- Return the filtered elements as a list.
Python3
test_list = [ 56 , 72 , 875 , 9 , 173 ]
print ( "The original list is : " + str (test_list))
K = 7
res = list ( filter ( lambda x: str (K) in str (x), map ( str , test_list)))
print ( "Elements with digit K : " + str (res))
|
Output
The original list is : [56, 72, 875, 9, 173]
Elements with digit K : ['72', '875', '173']
Time complexity: O(n)
Auxiliary Space: O(n)
Method #7 : Using operator.contains() method
Approach :
- Initiate a for loop to traverse list of numbers
- Convert each number to string and K to string, check whether string K is present in string number using operator.contains()
- If yes append number to output list
- Display output list
Python3
test_list = [ 56 , 72 , 875 , 9 , 173 ]
print ( "The original list is : " + str (test_list))
K = 7
res = []
import operator
for i in test_list:
x = str (i)
if (operator.contains(x, str (K))):
res.append(i)
print ( "Elements with digit K : " + str (res))
|
Output
The original list is : [56, 72, 875, 9, 173]
Elements with digit K : [72, 875, 173]
Time Complexity : O(N), N – length of test_list
Auxiliary Space: O(N), N – length of output list
Method 8: Using the string module to convert the integer K to a string and then using the in operator
Step-by-step approach:
- Import the string module.
- Define the original list test_list and print it.
- Initialize the value of K.
- Convert K to a string using the str() function and store it in the variable K_str.
- Use list comprehension to iterate over the elements of the list test_list and check if the string representation of K is present in the string representation of each element using the in operator. Append the element to the list res if it satisfies the condition.
- Print the list res.
Python3
import string
test_list = [ 56 , 72 , 875 , 9 , 173 ]
print ( "The original list is : " + str (test_list))
K = 7
K_str = str (K)
res = [i for i in test_list if K_str in str (i)]
print ( "Elements with digit K : " + str (res))
|
Output
The original list is : [56, 72, 875, 9, 173]
Elements with digit K : [72, 875, 173]
Time complexity: O(nm), where n is the length of the list test_list and m is the length of the string representation of the largest number in the list.
Auxiliary space: O(k), where k is the number of elements in the list that satisfy the condition.
Share your thoughts in the comments
Please Login to comment...