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
# 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) |
Output:
initial list [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] resultant sum 15
Method #2: Using try and except
# 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) |
Output:
initial list [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] resultant sum 15
Method #3: Using isinstance and conditional statements
# 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) |
Output:
initial list [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] resultant sum 15
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


