Python | Add only numeric values present in a list
Given a list containing characters and numbers, the task is to add only numbers from a list. Given below are a few methods to complete a given task.
Method #1: Using filter() and lambda
Python3
# Python code to demonstrate# how to add only numbers present# in a list of characters and numbers# initialising listsini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']# printing initial listprint("initial list", str(ini_list))# code to add numbers from listres = sum(filter(lambda i: isinstance(i, int), ini_list))# printing resultprint("resultant sum", res) |
initial list [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] resultant sum 15
Time Complexity: O(n), where n is the number of elements in the list ini_list.
Auxiliary Space: O(n), where n is the number of elements in the list ini_list. This is because the filter function creates a new list with the filtered elements, which takes up additional memory space.
Method #2: Using try and except
Python3
# Python code to demonstrate# how to add only numbers present# in a list of characters and numbers# initialising listsini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']# printing initial listprint("initial list", str(ini_list))# code to add numbers from listres = 0for item in ini_list: try: res += int(item) except ValueError: pass# printing resultprint("resultant sum", res) |
initial list [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] resultant sum 15
Method #3: Using isinstance and conditional statements
Python3
# Python code to demonstrate# how to add only numbers present# in a list of characters and numbers# initialising listsini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']# printing initial listprint("initial list", str(ini_list))# code to add numbers from listres = sum([x for x in ini_list if isinstance(x, int)])# printing resultprint("resultant sum", res) |
initial list [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] resultant sum 15
Method #4: Using type() and find() methods
Python3
# Python code to demonstrate# how to add only numbers present# in a list of characters and numbers# initialising listsini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']# printing initial listprint("initial list", str(ini_list))# code to add numbers from listres = 0for i in ini_list: p = str(type(i)) if(p.find('int') != -1): res += int(i)# printing resultprint("resultant sum", res)# contributed by Bhavya Koganti |
initial list [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] resultant sum 15
Method #5 : Using list(),map(),isnumeric() and int() methods
Python3
# Python code to demonstrate# how to add only numbers present# in a list of characters and numbers# initialising listsini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']# printing initial listprint("initial list", str(ini_list))x = list(map(str, ini_list))# code to add numbers from listres = 0for i in x: if i.isnumeric(): res += int(i)# printing resultprint("resultant sum", res) |
initial list [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] resultant sum 15
Method #6: Using regex
Here is an example of using the re (regular expression) module to sum the numeric values in a list:
The regular expression s used to check if a string contains only numeric characters. The ^ and $ symbols indicate that the string must match the pattern from start to end (i.e., it must contain only numeric characters, with no other characters before or after).
Python3
import redef add_numeric_values(lst): # Initialize sum to 0 sum = 0 # Iterate through the list for element in lst: # Check if the element is a number using regex if re.match(r"-?\d+", str(element)): # If it is a number, add it to the sum sum += element # Return the sum return sum#Test the functionlst = [11, 2, 3, 4, 'a', 'b', 'x', 5, 'z']print(add_numeric_values(lst)) # Output: 25#Test with an empty listprint(add_numeric_values([])) # Output: 0#Test with a list of all numeric valueslst = [1, 2, 3, 4, 5]print(add_numeric_values(lst)) # Output: 15#Test with a list of all non-numeric valueslst = ['a', 'b', 'c']print(add_numeric_values(lst)) # Output: 0#This code is contributed by Edula Vinay Kumar Reddy |
25 0 15 0
Time complexity: O(n*k), where n is the number of elements in the list, k is average time taken to check string or not.
Auxiliary space: O(n), as we need to store the string representation of the list and the list of numeric values.






Please Login to comment...