# Python – Extract Strings with a digit

• Last Updated : 07 Oct, 2021

Given a Strings List, extract those with atleast one digit.

Input : test_list = [‘gf4g’, ‘is’, ‘best’, ‘gee1ks’]
Output : [‘gf4g’, ‘gee1ks’]
Explanation : 4, 1 are respective digits in string.

Input : test_list = [‘gf4g’, ‘is’, ‘best’, ‘geeks’]
Output : [‘gf4g’]
Explanation : 4 is digit in string.

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

In this iteration for each string is done using list comprehension, any() and isdigit() is used for task of checking at least one digit.

## Python3

 # Python3 code to demonstrate working of# Extract Strings with a digit# Using list comprehension + any() + isdigit() # initializing listtest_list = ['gf4g', 'is', 'best', '4', 'gee1ks'] # printing original listprint("The original list is : " + str(test_list)) # checking if string contain any string using any()res = [sub for sub in test_list if any(ele for ele in sub if ele.isdigit())] # printing resultprint("Strings with any digit : " + str(res))

Output:

The original list is : [‘gf4g’, ‘is’, ‘best’, ‘4’, ‘gee1ks’]
Strings with any digit : [‘gf4g’, ‘4’, ‘gee1ks’]

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

In this, we perform task of filtering using lambda and filter(), rest remains the same.

## Python3

 # Python3 code to demonstrate working of# Extract Strings with a digit# Using any() + filter() + lambda # initializing listtest_list = ['gf4g', 'is', 'best', '4', 'gee1ks'] # printing original listprint("The original list is : " + str(test_list)) # checking if string contain any string using any()# filter() used to filter strings with digitsres = list(filter(lambda sub: any(    ele for ele in sub if ele.isdigit()), test_list)) # printing resultprint("Strings with any digit : " + str(res))

Output:

The original list is : [‘gf4g’, ‘is’, ‘best’, ‘4’, ‘gee1ks’]
Strings with any digit : [‘gf4g’, ‘4’, ‘gee1ks’]

My Personal Notes arrow_drop_up