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
ini_list = [ 1 , 2 , 3 , 4 , 'a' , 'b' , 'x' , 5 , 'z' ]
print ( "initial list" , str (ini_list))
res = sum ( filter ( lambda i: isinstance (i, int ), ini_list))
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
ini_list = [ 1 , 2 , 3 , 4 , 'a' , 'b' , 'x' , 5 , 'z' ]
print ( "initial list" , str (ini_list))
res = sum ([x for x in ini_list if isinstance (x, int )])
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
ini_list = [ 1 , 2 , 3 , 4 , 'a' , 'b' , 'x' , 5 , 'z' ]
print ( "initial list" , str (ini_list))
res = 0
for i in ini_list:
p = str ( type (i))
if (p.find( 'int' ) ! = - 1 ):
res + = int (i)
print ( "resultant sum" , res)
|
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
ini_list = [ 1 , 2 , 3 , 4 , 'a' , 'b' , 'x' , 5 , 'z' ]
print ( "initial list" , str (ini_list))
x = list ( map ( str , ini_list))
res = 0
for i in x:
if i.isnumeric():
res + = int (i)
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):
sum = 0
for element in lst:
if re.match(r "-?\d+" , str (element)):
sum + = element
return sum
lst = [ 11 , 2 , 3 , 4 , 'a' , 'b' , 'x' , 5 , 'z' ]
print (add_numeric_values(lst))
print (add_numeric_values([]))
lst = [ 1 , 2 , 3 , 4 , 5 ]
print (add_numeric_values(lst))
lst = [ 'a' , 'b' , 'c' ]
print (add_numeric_values(lst))
|
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
Share your thoughts in the comments
Please Login to comment...