Given List of numbers, extract all numbers with only similar digit.
Input : test_list = [463, 888, 123, ‘aaa’, 112, 111, ‘gfg’, 939, 4, ‘ccc’]
Output : [888, ‘aaa’, 111, 4, ‘ccc’]
Explanation : All elements having single unique digit or character.
Input : test_list = [463, “GFG”, 8838, 43, 991]
Output : []
Explanation : No element found to be having only single digit.
Method #1 : Using list comprehension + all()
In this, we iterate all the elements using list comprehension, all() is used to check equality of all digits with first digit.
Python3
test_list = [ 463 , 888 , 123 , "aaa" , 112 , 111 , "gfg" , 939 , 4 , "ccc" ]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if all (
str (ele) = = str (sub)[ 0 ] for ele in str (sub))]
print ( "Extracted Numbers : " + str (res))
|
Output:
The original list is : [463, 888, 123, ‘aaa’, 112, 111, ‘gfg’, 939, 4, ‘ccc’] Extracted Numbers : [888, ‘aaa’, 111, 4, ‘ccc’]
Time complexity: O(n * k), where n is the length of the list and k is the maximum number of digits in a single element of the list.
Auxiliary space: O(n), as we are creating a new list to store the extracted monodigit elements.
Method #2 : Using filter() + lambda + all()
In this, we perform task of filtering using lambda function, filter(), and all() is again used to check equality of all digits.
Python3
test_list = [ 463 , 888 , 123 , "aaa" , 112 , 111 , "gfg" , 939 , 4 , "ccc" ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: all ( str (ele) = = str (
sub)[ 0 ] for ele in str (sub)), test_list))
print ( "Extracted Numbers : " + str (res))
|
Output:
The original list is : [463, 888, 123, ‘aaa’, 112, 111, ‘gfg’, 939, 4, ‘ccc’] Extracted Numbers : [888, ‘aaa’, 111, 4, ‘ccc’]
Time complexity: O(n * k) where n is the length of the input list and k is the maximum number of digits in a number in the list.
Auxiliary space: O(k) where k is the maximum number of digits in a number in the list, for creating the lambda function.
Method #3 : Using list(),map(),count(),len()
Initially convert every element of list to string.Now iterate over list and iterate over each string in list, check whether the occurrence of first element is equal to length of list.If it is True then the elements of list are having mono digits.
Python3
test_list = [ 463 , 888 , 123 , "aaa" , 112 , 111 , "gfg" , 939 , 4 , "ccc" ]
print ( "The original list is : " + str (test_list))
x = list ( map ( str ,test_list))
res = []
for i in range ( 0 , len (x)):
if (x[i].count(x[i][ 0 ]) = = len (x[i])):
res.append(test_list[i])
print ( "Extracted Numbers : " + str (res))
|
Output
The original list is : [463, 888, 123, 'aaa', 112, 111, 'gfg', 939, 4, 'ccc']
Extracted Numbers : [888, 'aaa', 111, 4, 'ccc']
Time complexity: O(n * k) where n is the length of the input list and k is the maximum number of digits in a number in the list.
Auxiliary space: O(k) where k is the maximum number of digits in a number in the list, for creating the lambda function.
Method #4 : Using list(),map(),len() methods and * operator
Python3
test_list = [ 463 , 888 , 123 , "aaa" , 112 , 111 , "gfg" , 939 , 4 , "ccc" ]
print ( "The original list is : " + str (test_list))
x = list ( map ( str ,test_list))
res = []
for i in range ( 0 , len (x)):
a = x[i][ 0 ] * len (x[i])
if (a = = x[i]):
res.append(test_list[i])
print ( "Extracted Numbers : " + str (res))
|
Output
The original list is : [463, 888, 123, 'aaa', 112, 111, 'gfg', 939, 4, 'ccc']
Extracted Numbers : [888, 'aaa', 111, 4, 'ccc']
Method #5 : Using list(),map(),operator.countOf(),len() methods
Python3
test_list = [ 463 , 888 , 123 , "aaa" , 112 , 111 , "gfg" , 939 , 4 , "ccc" ]
print ( "The original list is : " + str (test_list))
x = list ( map ( str ,test_list))
res = []
for i in range ( 0 , len (x)):
import operator
if (operator.countOf(x[i],x[i][ 0 ]) = = len (x[i])):
res.append(test_list[i])
print ( "Extracted Numbers : " + str (res))
|
Output
The original list is : [463, 888, 123, 'aaa', 112, 111, 'gfg', 939, 4, 'ccc']
Extracted Numbers : [888, 'aaa', 111, 4, 'ccc']
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #6: Using a for loop and string conversion
We can also solve this problem using a simple for loop and string conversion. We will convert each element of the list to a string and check if all the characters are the same. If they are the same, we will append the element to a new list.
steps
Initialize an empty list to store the extracted elements.
Iterate over each element of the input list using a for loop.
Convert the element to a string using the str() function.
Check if all the characters of the string are the same using the set() function. If the length of the set is 1, it means that all the characters are the same.
If all the characters are the same, append the element to the new list.
Return the new list containing the extracted elements.
Python3
test_list = [ 463 , 888 , 123 , "aaa" , 112 , 111 , "gfg" , 939 , 4 , "ccc" ]
print ( "The original list is : " + str (test_list))
res = []
for elem in test_list:
elem_str = str (elem)
if len ( set (elem_str)) = = 1 :
res.append(elem)
print ( "Extracted Numbers : " + str (res))
|
Output
The original list is : [463, 888, 123, 'aaa', 112, 111, 'gfg', 939, 4, 'ccc']
Extracted Numbers : [888, 'aaa', 111, 4, 'ccc']
Time complexity: O(n*k), where n is the length of the input list and k is the average length of the elements in the list.
Auxiliary space: O(1), as we are not using any extra space apart from the output list.
Share your thoughts in the comments
Please Login to comment...