Open In App

Python | Add only numeric values present in a list

Improve
Improve
Like Article
Like
Save
Share
Report

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 lists
ini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']
 
# printing initial list
print("initial list", str(ini_list))
 
# code to add numbers from list
res = sum(filter(lambda i: isinstance(i, int), ini_list))
 
# printing result
print("resultant sum", res)


Output

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 #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 lists
ini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']
 
# printing initial list
print("initial list", str(ini_list))
 
# code to add numbers from list
res = sum([x for x in ini_list if isinstance(x, int)])
 
# printing result
print("resultant sum", res)


Output

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
Auxiliary space: O(1) as only a single variable is used to store the sum.

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 lists
ini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']
 
# printing initial list
print("initial list", str(ini_list))
 
# code to add numbers from list
res = 0
for i in ini_list:
    p = str(type(i))
    if(p.find('int') != -1):
        res += int(i)
# printing result
print("resultant sum", res)
 
# contributed by Bhavya Koganti


Output

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 lists
ini_list = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z']
 
# printing initial list
print("initial list", str(ini_list))
x = list(map(str, ini_list))
# code to add numbers from list
res = 0
for i in x:
    if i.isnumeric():
        res += int(i)
# printing result
print("resultant sum", res)


Output

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 re
def 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 function
lst = [11, 2, 3, 4, 'a', 'b', 'x', 5, 'z']
print(add_numeric_values(lst))  # Output: 25
#Test with an empty list
print(add_numeric_values([]))  # Output: 0
#Test with a list of all numeric values
lst = [1, 2, 3, 4, 5]
print(add_numeric_values(lst))  # Output: 15
#Test with a list of all non-numeric values
lst = ['a', 'b', 'c']
print(add_numeric_values(lst))  # Output: 0
#This code is contributed by Edula Vinay Kumar Reddy


Output

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.



Last Updated : 14 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads