This has two loops; one to examine each character and extract only the digits, and a second one to iterate over my_list: >>> my_list = [ ' 14,200 new items ' , '15,200 new items '] >>> [char for char in my_list[0] if char.isdigit()] ['1', '4', '2', '0', '0'] >>> "".join([char for char in my_list[0] if char.isdigit()]) '14200' >>> int("".join([char for char in my_list[0] if char.isdigit()])) 14200 >>> def extract_int(istr): return int("".join([char for char in istr if char.isdigit()])) ... >>> extract_int(my_list[0]) 14200 >>> [extract_int(phrase) for phrase in my_list] [14200, 15200] >>> Answer from efmccurdy on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to extracts number from this python list
r/learnpython on Reddit: How to extracts number from this python list
June 25, 2022 -

My list =[ ' 14,200 new items ' , '15,200 new items '] I tried for loop with isdigit to extract number But output was blank list .

Remove numbers from list with list comprehension Dec 27, 2021
r/learnpython
4y ago
how do I extract digits from a string? Aug 30, 2022
r/learnpython
3y ago
How do I manipulate string values inside a list? Mar 30, 2023
r/learnpython
3y ago
How to convert string list to int list? Jun 4, 2023
r/learnpython
3y ago
More results at reddit.com
Discussions

Python Extracting numbers and adding them from a list - Stack Overflow
I have a list of strings: [' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 miles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n', '.7 miles', '5.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Extract numbers from a string list - python - Stack Overflow
If I have a list like this [ [100], [500], [300] ], what's the best way in python to extract the numbers from it? result = [ 100, 500, 300 ] More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Extract numbers from list of lists - Stack Overflow
Explanation: I loop over the lists (line) in you list, then loop over the desired keywords ('garden' and 'house'), then I check if that keyword is in the line, if so, I loop over line's element to find an element which starts with a number (this is my assumption), if so, I append it to the ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
extracting numbers from list of strings with python - Stack Overflow
I have a list of strings that I am trying to parse for data that is meaningful to me. I need an ID number that is contained within the string. Sometimes it might be two or even three of them. Example More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 5
2

Using re.search and a regex pattern, you can extract the integer value from the string.

>>> import re
>>> 
>>> re.search(r'\d+', data[9])
<_sre.SRE_Match object; span=(16, 19), match='102'>
>>> re.search(r'\d+', data[9]).group(0)
'102'
>>> speed = int(re.search(r'\d+', data[9]).group(0))
>>> speed
102

Here, re.search(r'\d+', data[9]) will return a match if data[9] has any decimal characters. Then retrieving this decimal characters using .group(0) and finally convert them to integer, using int() built-in method

2 of 5
2
>>> int(data[9][-4:-1])+1
103

Use iterable[start:end] to get a subset of them.

Another thing you can do is, if you want to get a single integer from a string, is something along the lines of (python2.x):

s = "test123\n"
x = int(filter(lambda x: x.isdigit(), s))

In Python3, filter returns a filter object which is an iterable. You can turn it into a list and join them for the same functional result.

s = "test123\n"
x = int(''.join(list(filter(lambda x: x.isdigit(), s))))

Note that this does not take into account separated numbers. For example, s = "123test456" will return 123456. If you want them separated, you will need to use something a bit more clever.

>>> s = "\n123test456\n"
>>> nums = map(int, ''.join([x if x.isdigit() else ' ' for x in s]).split())
>>> print(nums)
[123, 456]

Again, in python3, map returns a map object, so you just have to convert it to a list.

>>> s = "\n123test456\n"
>>> nums = list(map(int, ''.join([x if x.isdigit() else ' ' for x in s]).split()))
>>> print(nums)
[123, 456]
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-extract-numbers-from-list-of-strings
Python โ€“ Extract numbers from list of strings | GeeksforGeeks
January 30, 2025 - It extracts and converts digit words into integers, adding them to the list n. This method uses filter() to extract numbers from each string by checking if the string represents a digit.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ extract-numbers-from-list-of-strings-in-python
Extract numbers from list of strings in Python
May 5, 2020 - days_with_numbers = ['Mon-2', 'Wed-8', 'Thu-2', 'Fri-7'] # Given list print("Given list : " + str(days_with_numbers)) # Extracting numbers using split num_list = [int(item.split('-')[1]) for item in days_with_numbers] # print result print("List only with numbers : ", num_list)
๐ŸŒ
Know Program
knowprogram.com โ€บ home โ€บ how to extract an element from a list in python
How to Extract an Element from a List in Python - Know Program
March 16, 2022 - Also See:- How to Find the Length of a List in Python ยท We will see these below Python program examples:โ€“ ... Here, we extract a number from the list string, we have used the split() method to extract the numbers.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to extract numbers from a string in python?
How To Extract Numbers From A String In Python? - Be on the Right Side of Change
February 25, 2023 - Thus to solve our problem, we must import the regex module, which is already included in Pythonโ€™s standard library, and then with the help of the findall() function we can extract the numbers from the given string.
Find elsewhere
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ 5 easy ways to extract elements from a python list
5 Easy Ways To Extract Elements From A Python List - AskPython
December 29, 2021 - The range function creates a list containing numbers serially with 6 elements in it from 10 to 15. numbers = range(10, 16) indices = (1, 1, 2, 1, 5) result = [numbers[i] for i in indices] print(result) ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-extract-numbers-from-list-of-strings
Python - Extract numbers from list of strings - GeeksforGeeks
July 11, 2025 - It extracts and converts digit words into integers, adding them to the list n. This method uses filter() to extract numbers from each string by checking if the string represents a digit.
๐ŸŒ
Python Shiksha
python.shiksha โ€บ home โ€บ tips โ€บ 3 ways to get the number of elements inside a python list
3 ways to get the number of elements inside a Python list - Python Shiksha
July 18, 2021 - When a object is passed to the len() function as an argument, the internal __len__() function is called and the number elements inside the data structure is counted. Surprisingly, this len() functions works on every sequential data structure ...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python extract number from string
How to Extract Numbers From a String in Python | Delft Stack
March 11, 2025 - The function extract_numbers takes an input string and uses re.findall() to search for all occurrences of one or more digits (\d+). The result is a list of strings containing the numbers found in the input string.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-program-to-extract-only-the-numbers-from-a-list-which-have-some-specific-digits
Python program to extract only the numbers from a list which have some specific digits
When it is required to extract only the numbers from a list which have some specific digits, a list comprehension and the โ€˜allโ€™ operator is used. Below is a demonstration of the same โˆ’ Example
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-extract-only-the-numbers-from-a-list-which-have-some-specific-digits
Python program to extract only the numbers from a list which have some specific digits - GeeksforGeeks
July 23, 2025 - # Python3 code to demonstrate working of # Elements with specific digits # Using filter() + lambda + all() # initializing list test_list = [345, 23, 128, 235, 982] # printing original list print("The original list is : " + str(test_list)) # initializing digit list dig_list = [2, 3, 5, 4] # filter() used to filter from logic res = list(filter(lambda sub: all( int(ele) in dig_list for ele in str(sub)), test_list)) # printing result print("Extracted elements : " + str(res))
Top answer
1 of 4
1

The problem is with your regex. You need to remove the $ anchor, which will fail to match a number if anything (e.g. the % character) is following the expected characters, namely [0-9,.]. The rest of the rest of the code can be simplified a bit, too:

import re
from collections import defaultdict

my_list=[['word:', 'house', 'garden', '0,2%'],
 ['word:', 'house', 'garden', '0,2%'],
 ['house', 'garden', '0,2%'],
 ['house', 'garden', '0,2%'],
 ['garden', '0,2%', '0,125%'],
 ['house', '0,2%', '?????'],
 ['house', 'garden', '0,02%'],
 ['house', 'garden', '0,02%'],
 ['garden', '0,02%'],
 ['house', 'garden', '0,2%'],
 ['garden', '0,2%'],
 ['house', '0,2'],
 ['house', '0,2', '%'],
 ['house', 'garden', 'kids', '0,2%'],
 ['house', 'garden', 'kids', '0,2%'],
 ['house', '0,2%', 'boy'],
 ['house', '0,12%'],
 ['house', '4%.'],
 ['house', '4%.', '4.'],
 ['house', '0,2%".']]

result = defaultdict(list)
keywords = ['house', 'garden']
for l in my_list:
    numbers = [v for v in l if re.match(r'[0-9,.]+', v)]
    for v in l:
        if v in keywords:
            result[v].extend(numbers)
print(result)

Prints:

defaultdict(<class 'list'>, {'house': ['0,2%', '0,2%', '0,2%', '0,2%', '0,2%', '0,02%', '0,02%', '0,2%', '0,2', '0,2', '0,2%', '0,2%', '0,2%', '0,12%', '4%.', '4%.', '4.', '0,2%".'], 'garden': ['0,2%', '0,2%', '0,2%', '0,2%', '0,2%', '0,125%', '0,02%', '0,02%', '0,02%', '0,2%', '0,2%', '0,2%', '0,2%']})
2 of 4
0

Here is what you can do:

my_list=[['word:', 'house', 'garden', '0,2%'],
         ['word:', 'house', 'garden', '0,2%'],
         ['house', 'garden', '0,2%'],
         ['house', 'garden', '0,2%'],
         ['garden', '0,2%', '0,125%'],
         ['house', '0,2%', '?????'],
         ['house', 'garden', '0,02%'],
         ['house', 'garden', '0,02%'],
         ['garden', '0,02%'],
         ['house', 'garden', '0,2%'],
         ['garden', '0,2%'],
         ['house', '0,2'],
         ['house', '0,2', '%'],
         ['house', 'garden', 'kids', '0,2%'],
         ['house', 'garden', 'kids', '0,2%'],
         ['house', '0,2%', 'boy'],
         ['house', '0,12%'],
         ['house', '4%.'],
         ['house', '4%.', '4.'],
         ['house', '0,2%โ€.']]


my_dict = { 'garden':[], 'house':[]}
for lst in my_list:
    for s in lst:
        if any([n in s for n in '1234567890']):
            if 'house' in lst:
                my_dict['house'].append(s.replace('%',''))
            if 'garden' in lst:
                my_dict['garden'].append(s.replace('%',''))
print(my_dict)

Output:

{'garden': ['0,2', '0,2', '0,2', '0,2', '0,2', '0,125', '0,02', '0,02', '0,02', '0,2', '0,2', '0,2', '0,2'], 'house': ['0,2', '0,2', '0,2', '0,2', '0,2', '0,02', '0,02', '0,2', '0,2', '0,2', '0,2', '0,2', '0,2', '0,12', '4.', '4.', '4.', '0,2โ€.']}
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 398814 โ€บ q-extract-integers-from-a-list
python - Q: Extract integers from a list | DaniWeb
December 7, 2011 - It might be safer to extract the numeric value ... list1= ['NNW 30', 'SE 15', 'SSW 60', 'NNE 70', 'N 10'] list2 = [] for item in list1: s = "" for c in item: # extract numeric value if c in '1234567890.-': s += c if โ€ฆ
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-extract-numbers-from-string
Python | Extract Numbers from String - GeeksforGeeks
September 16, 2024 - test_string = "There are 2 apples for 4 persons" # printing original string print("The original string : " + test_string) # getting numbers from string res = [] x=test_string.split() for i in x: if i.isnumeric(): res.append(int(i)) # print result print("The numbers list is : " + str(res)) ... This particular problem can also be solved using Python regex, we can use the findall function to check for the numeric occurrences using a matching regex string. ... import re # initializing string test_string = "There are 2 apples for 4 persons" # printing original string print("The original string : " + test_string) # getting numbers from string temp = re.findall(r'\d+', test_string) res = list(map(int, temp)) # print result print("The numbers list is : " + str(res))
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ list โ€บ python-data-type-list-exercise-103.php
Python: Extract specified number of elements from a given list, which follows each other continuously - w3resource
June 28, 2025 - # Import the 'groupby' function from the 'itertools' module from itertools import groupby # Define a function 'extract_elements' that extracts elements from a list that follow each other continuously and occur 'n' times def extract_elements(nums, n): # Use a list comprehension and 'groupby' to filter elements in 'nums' that occur 'n' times in a row result = [i for i, j in groupby(nums) if len(list(j)) == n] return result # Create a list 'nums1' containing numbers nums1 = [1, 1, 3, 4, 4, 5, 6, 7] # Set the value of 'n' to 2 n = 2 # Print a message indicating the original list print("Original li