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.
Similar Reads
Python | Even Front digits Test in List
Sometimes we may face a problem in which we need to find a list if it contains numbers that are even. This particular utility has an application in day-day programming. Letâs discuss certain ways in which this task can be achieved. Method #1: Using list comprehension + map() We can approach this pro
5 min read
Python - Test rear digit match in all list elements
Sometimes we may face a problem in which we need to find a list if it contains numbers ending with the same digits. This particular utility has an application in day-day programming. Letâs discuss certain ways in which this task can be achieved. Method #1: Using list comprehension + map() We can app
6 min read
Python - Sort by Maximum digit in Element
Given a List of Elements, sort by the maximum digit of the element present in the List. Input : test_list = [234, 92, 8, 721] Output : [234, 721, 8, 92] Explanation : 4 < 7 < 8 < 9, sorted by maximum digits. Input : test_list = [92, 8, 721] Output : [721, 8, 92] Explanation : 7 < 8 <
6 min read
Python - Match Kth number digit in list elements
Sometimes we may face a problem in which we need to find a list if it contains numbers at the Kth index with the same digits. This particular utility has an application in day-day programming. Letâs discuss certain ways in which this task can be achieved. Method #1 : Using list comprehension + map()
7 min read
Python - Reform K digit elements
Given the Python list, reform the element to have K digits in a single element. Input : test_list = [223, 67, 332, 1, 239, 2, 931], K = 2 Output : [22, 36, 73, 32, 12, 39, 29, 31] Explanation : Elements reformed to assign 2 digits to each element. Input : test_list = [223, 67, 3327], K = 3 Output :
3 min read
Python - Elements with same index
Given a List, get all elements that are at their index value. Input : test_list = [3, 1, 8, 5, 4, 10, 6, 9] Output : [1, 4, 6] Explanation : These elements are at same position as its number.Input : test_list = [3, 10, 8, 5, 14, 10, 16, 9] Output : [] Explanation : No number at its index. Method #1:
5 min read
Python - Append given number with every element of the list
In Python, lists are flexible data structures that allow us to store multiple values in a single variable. Often, we may encounter situations where we need to modify each element of a list by appending a given number to it. Given a list and a number, write a Python program to append the number with
5 min read
Python | Every Kth element in list
Sometimes, while working with Python lists, we can have a problem in which we require to extract every Kth element of list and slice out a new list from that. This type of problems are quite common as a variation of list slicing. Let's discuss a way in which this can be done. Method : Using list sli
3 min read
Python - Extract tuples having K digit elements
Given a list of tuples, extract all tuples having K digit elements. Input : test_list = [(54, 2), (34, 55), (222, 23), (12, 45), (78, )], K = 2 Output : [(34, 55), (12, 45), (78,)] Explanation : All tuples have numbers with 2 digits. Input : test_list = [(54, 2), (34, 55), (222, 23), (12, 45), (782,
6 min read
Python | Get Last N Elements from Given List
This article discusses ways to fetch the last N elements of a list in Python. Example: Using list Slicing [GFGTABS] Python test_list = [4, 5, 2, 6, 7, 8, 10] # initializing N N = 5 # using list slicing res = test_list[-N:] print(str(res)) [/GFGTABS]Output[2, 6, 7, 8, 10] Explaination: This problem c
3 min read