Python – Extract Monodigit elements
Last Updated :
30 Apr, 2023
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.
Similar Reads
Python - K middle elements
Given a List, extract K elements occurring in middle of list. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5, 9], K = 3 Output : [7, 8, 5] Explanation : Middle 3 elements are extracted. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5, 9], K = 7 Output : [3, 5, 7, 8, 5, 3, 5] Explanation : Middle 7 elements
5 min read
Python - Extract ith element of K key's value
Given a dictionary, extract ith element of K key's value list. Input : test_dict = {'Gfg' : [6, 7, 3, 1], 'is' : [9, 1, 4], 'best' : [10, 7, 4]}, K = 'Gfg', i = 1 Output : 7 Explanation : 1st index of 'Gfg''s value is 7.Input : test_dict = {'Gfg' : [6, 7, 3, 1], 'is' : [9, 1, 4], 'best' : [10, 7, 4]
6 min read
Python - Extract dictionary items with List elements
Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract all the items from dictionary that constitute all the elements from a list. This kind of problem can occur in domains such as competitive programming and web development. Let's discuss certain ways i
8 min read
Python - Extract Kth index elements from Dictionary Value list
Given a dictionary with list as values, extract all the Kth index elements. Input : {"Gfg" : [4, 7, 5], "Best" : [8, 6, 7], "is" : [9, 3, 8]}, K = 2 Output : [5, 7, 8] Explanation : The 2nd index elements are 5, 7 and 8 respectively in different keys. Input : {"Gfg" : [4, 7, 5], "Best" : [8, 6, 7],
7 min read
Python - Extract digits from Tuple list
Sometimes, while working with Python lists, we can have a problem in which we need to perform extraction of all the digits from tuple list. This kind of problem can find its application in data domains and day-day programming. Let's discuss certain ways in which this task can be performed. Input : t
6 min read
Python | Extract Strings with only Alphabets
In Python, extracting strings that contain only alphabetic characters involves filtering out any strings that include numbers or special characters. The most efficient way to achieve this is by using the isalpha() method, which checks if all characters in a string are alphabetic. [GFGTABS] Python li
3 min read
Python - Rear element extraction from list of tuples records
While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print specific information from the tuple like rear index. For instance, a piece of code would want just names to be printed on all the student data. Let's discuss certain ways in which one
8 min read
Python - Extract digits from given string
We need to extract the digit from the given string. For example we are given a string s=""abc123def456gh789" we need to extract all the numbers from the string so the output for the given string will become "123456789" In this article we will show different ways to extract digits from a string in Py
2 min read
Python - Extract date in String
Given a string, the task is to write a Python program to extract date from it. Input : test_str = "gfg at 2021-01-04" Output : 2021-01-04 Explanation : Date format string found. Input : test_str = "2021-01-04 for gfg" Output : 2021-01-04 Explanation : Date format string found. Method #1 : Using re.s
4 min read
Split Elements of a List in Python
Splitting elements of a list in Python means dividing strings inside each list item based on a delimiter. split() Method is the easy and most straightforward method to split each element is by using the split() method. This method divides a string into a list based on a delimiter. Here's how we can
2 min read