Skip to content
Related Articles

Related Articles

Improve Article
Python – Extract Monodigit elements
  • Last Updated : 11 Oct, 2020

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




# Python3 code to demonstrate working of
# Extract Monodigit elements
# Using list comprehension + all()
  
# initializing list
test_list = [463, 888, 123, "aaa", 112, 111, "gfg", 939, 4, "ccc"]
  
# printing original lists
print("The original list is : " + str(test_list))
  
# all() checks for all similar digits
res = [sub for sub in test_list if all(
    str(ele) == str(sub)[0] for ele in str(sub))]
  
# printing result
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 #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




# Python3 code to demonstrate working of
# Extract Monodigit elements
# Using filter() + lambda + all()
  
# initializing list
test_list = [463, 888, 123, "aaa", 112, 111, "gfg", 939, 4, "ccc"]
  
# printing original lists
print("The original list is : " + str(test_list))
  
# all() checks for all similar digits
# filter() used for filtering
res = list(filter(lambda sub: all(str(ele) == str(
    sub)[0] for ele in str(sub)), test_list))
  
# printing result
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’]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :