Python - Extract numbers from list of strings Last Updated : 30 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We are given a list of string we need to extract numbers from the list of string. For example we are given a list of strings s = ['apple 123', 'banana 456', 'cherry 789'] we need to extract numbers from the string in list so that output becomes [123, 456, 789].Using Regular ExpressionsThis method uses re.findall() to capture all digit sequences in each string. Python import re s = ['apple 123', 'banana 456', 'cherry 789'] n = [] for string in s: # Convert each found number to an integer and extend the result into the list n n.extend(map(int, re.findall(r'\d+', string))) print(n) Output[123, 456, 789] Explanation:re.findall(r'\d+', s) function is used to find all sequences of digits (numbers) in each string from the list s. It returns a list of matched numbers as strings.map(int, ...) function converts each found number from string to integer and the extend() method adds them to the list n. The result is a list of all extracted integers from the strings.Using List Comprehension with isdigit()This method checks for numbers by iterating through each string and extracting numbers manually. Python s = ['apple 123', 'banana 456', 'cherry 789'] n = [] for string in s: # Convert the filtered digit words into integers and extend them into the list n n.extend([int(word) for word in string.split() if word.isdigit()]) print(n) Output[123, 456, 789] Explanation:Code incorrectly uses s.split() instead of string.split() to split each string into words.It extracts and converts digit words into integers, adding them to the list n.Using filter() and isdigit()This method uses filter() to extract numbers from each string by checking if the string represents a digit. Python s = ['apple 123', 'banana 456', 'cherry 789'] n = [] for string in s: # Convert and add the numbers to the list n n.extend(map(int, filter(str.isdigit, string.split()))) print(n) Output[123, 456, 789] Explanation:Code splits each string in the list into words and filters out those that are digits using isdigit().Valid digit words are converted to integers and added to the n list using extend(). Comment More infoAdvertise with us Next Article Python - Extract numbers from list of strings M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python | Extract Numbers in Brackets in String Sometimes, while working with Python strings, we can have a problem in which we have to perform the task of extracting numbers in strings that are enclosed in brackets. Let's discuss the certain ways in which this task can be performed. Method 1: Using regex The way to solve this task is to construc 6 min read Python program to extract numeric suffix from string Given a string of characters with digits embedded in it. The task is to write a Python program to extract all the numbers that are trailing, i.e at the suffix of the string. Examples:Input : test_str = "GFG04"Output : 04Explanation : 04 is suffix of string and number hence extracted.Input : test_str 7 min read Python - Extract Percentages from String Given a String, extract all the numbers that are percentages. Input : test_str = 'geeksforgeeks 20% is 100% way to get 200% success' Output : ['20%', '100%', '200%'] Explanation : 20%, 100% and 200% are percentages present. Input : test_str = 'geeksforgeeks is way to get success' Output : [] Explana 2 min read Python | Convert Stream of numbers to list Sometimes, we can be stuck with a problem in which we are given a stream of space separated numbers with a goal to convert them into a list of numbers. This type of problem can occur in common day-day programming or competitive programming while taking inputs. Let's discuss certain ways in which thi 5 min read Extract Substrings From A List Into A List In Python Python is renowned for its simplicity and versatility, making it a popular choice for various programming tasks. When working with lists, one common requirement is to extract substrings from the elements of the list and organize them into a new list. In this article, we will see how we can extract s 2 min read Like