# Python – Extract Strings with a digit

• Last Updated : 30 Nov, 2022

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 list` `test_list ``=` `[``'gf4g'``, ``'is'``, ``'best'``, ``'4'``, ``'gee1ks'``]`   `# printing original list` `print``(``"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 result` `print``(``"Strings with any digit : "` `+` `str``(res))`

Output

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

Time Complexity: O(n2)
Space Complexity: O(n)

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 list` `test_list ``=` `[``'gf4g'``, ``'is'``, ``'best'``, ``'4'``, ``'gee1ks'``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# checking if string contain any string using any()` `# filter() used to filter strings with digits` `res ``=` `list``(``filter``(``lambda` `sub: ``any``(` `    ``ele ``for` `ele ``in` `sub ``if` `ele.isdigit()), test_list))`   `# printing result` `print``(``"Strings with any digit : "` `+` `str``(res))`

Output

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

Time Complexity: O(n2
Space Complexity: O(n)

Method #3 : Without any builtin methods

## Python3

 `# Python3 code to demonstrate working of` `# Extract Strings with a digit`     `def` `fun(s):` `    ``digits ``=` `"0123456789"` `    ``x ``=` `s` `    ``for` `i ``in` `digits:` `        ``s ``=` `s.replace(i, "")` `    ``if``(``len``(x) !``=` `len``(s)):` `        ``return` `True` `    ``return` `False`     `# initializing list` `test_list ``=` `[``'gf4g'``, ``'is'``, ``'best'``, ``'4'``, ``'gee1ks'``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))` `res ``=` `[]` `for` `i ``in` `test_list:` `    ``if` `fun(i):` `        ``res.append(i)`   `# printing result` `print``(``"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
Related Articles