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
# 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))
OutputThe 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))
OutputThe 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))
OutputThe 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
OutputThe 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))
OutputThe 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))
OutputThe 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
# 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))
OutputThe 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))
OutputThe 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 | Remove given element from the list
Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 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 - Ways to format elements of given list
Formatting elements of a list is a common requirement especially when displaying or preparing data for further processing. Python provides multiple methods to format list elements concisely and efficiently. List comprehension combined with Python f-strings provides a simple and efficient way to form
2 min read
Get the Last Element of List in Python
In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list:Input: list = [1, 3, 34, 12, 6]Output: 6Explanation: Last element of the list l in the above example is 6.Let's explore various methods of doing it in Python:1. Using Neg
2 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
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