Python – Extract Tuples with all Numeric Strings

Given a list of tuples, extract only those tuples which have all numeric strings.

Input : test_list = [(“45”, “86”), (“Gfg”, “1”), (“98”, “10”)]
Output : [(’45’, ’86’), (’98’, ’10’)]
Explanation : Only number representating tuples filtered.

Input : test_list = [(“Gfg”, “1”)]
Output : []
Explanation : No tuple containing just numbers.

Method #1 : Using list comprehension + all() + isdigit()

In this, we check for string being numeric string using isdigit() and all() is used to check for all strings. List comprehension is used to iterate all tuples.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Tuples with all Numeric Strings
# Using all() + list comprehension + isdigit()
  
# initializing list
test_list = [("45", "86"), ("Gfg", "1"), ("98", "10"), ("Gfg", "Best")]
  
# printing original list
print("The original list is : " + str(test_list))
  
# all() checks for all digits()
res = [sub for sub in test_list if all(ele.isdigit() for ele in sub)]
  
# printing result 
print("Filtered Tuples : " + str(res))

chevron_right


Output

The original list is : [('45', '86'), ('Gfg', '1'), ('98', '10'), ('Gfg', 'Best')]
Filtered Tuples : [('45', '86'), ('98', '10')]

Method #2 : Using lambda + filter() + isdigit()

In this, we perform task of filtering using filter() + lambda, and isdigit() is used to check for numerics.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Tuples with all Numeric Strings
# Using lambda + filter() + isdigit()
  
# initializing list
test_list = [("45", "86"), ("Gfg", "1"), ("98", "10"), ("Gfg", "Best")]
  
# printing original list
print("The original list is : " + str(test_list))
  
# all() checks for all digits()
res = list(filter(lambda sub : all(ele.isdigit() for ele in sub), test_list))
  
# printing result 
print("Filtered Tuples : " + str(res))

chevron_right


Output

The original list is : [('45', '86'), ('Gfg', '1'), ('98', '10'), ('Gfg', 'Best')]
Filtered Tuples : [('45', '86'), ('98', '10')]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.