Friday, November 29, 2024
Google search engine
HomeLanguagesPython – List Elements with given digit

Python – List Elements with given digit

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




# Python3 code to demonstrate working of
# Elements with K digit
# Using list comprehension + str()
 
# initializing list
test_list = [56, 72, 875, 9, 173]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 7
 
# extracting all elements with digit K using
# in operator after string conversion using str()
res = [ele for ele in test_list if str(K) in str(ele)]
 
# printing result
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




# Python3 code to demonstrate working of
# Elements with K digit
# Using filter() + lambda + str()
 
# initializing list
test_list = [56, 72, 875, 9, 173]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 7
 
# using filter() and lambda to perform conditionals
# using str() to perform data type conversions
res = list(filter(lambda ele: str(K) in str(ele), test_list))
 
# printing result
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




# Python3 code to demonstrate working of
# Elements with K digit
 
# initializing list
test_list = [56, 72, 875, 9, 173]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 7
 
res=[]
for i in test_list:
    if(str(i).find(str(K))!=-1):
        res.append(i)
# printing result
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
# Initializing list
test_list = [56, 72, 875, 9, 173]
# Printing original list
print("The original list is:", test_list)
# Initializing K
K = 7
# Extracting elements with digit K using itertools.compress
res = list(itertools.compress(test_list, [str(K) in str(x) for x in test_list]))
# Printing result
print("Elements with digit K:", res)
#This is code is contributed by Jyothi pinjala


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




# Python3 code to demonstrate working of
# Elements with K digit
 
# initializing list
test_list = [56, 72, 875, 9, 173]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 7
 
res=[]
for i in test_list:
    x=str(i)
    y=str(K)
    z=x.replace(y,"")
    if(x!=z):
        res.append(i)
# printing result
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




# Python3 code to demonstrate working of
# Elements with K digit
 
# initializing list
test_list = [56, 72, 875, 9, 173]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 7
 
# using map() + str() + filter() method
res = list(filter(lambda x: str(K) in str(x), map(str, test_list)))
 
# printing result
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 : 

  1. Initiate a for loop to traverse list of numbers
  2. Convert each number to string and K to string, check whether string K is present in string number using operator.contains()
  3. If yes append number to output list
  4. Display output list

Python3




# Python3 code to demonstrate working of
# Elements with K digit
 
# initializing list
test_list = [56, 72, 875, 9, 173]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 7
 
res=[]
import operator
for i in test_list:
    x=str(i)
    if(operator.contains(x,str(K))):
        res.append(i)
     
# printing result
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
 
# initializing list
test_list = [56, 72, 875, 9, 173]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 7
 
# Method #8: Using string module
K_str = str(K)
res = [i for i in test_list if K_str in str(i)]
 
# printing result
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments