Add only Numeric Values Present in a List - Python Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report To sum only the numeric values in a list containing mixed data types, we iterate through the list, filter out non-numeric elements and then calculate the sum of the remaining numeric values. Using filter and sumOne efficient to handle this is by combining the filter() function with the sum() function. filter() function selects only the numeric elements such as integers or floats based on a specified condition and the sum() function then calculates the total of these filtered values. Python li = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] # sum to add only numeric values res = sum(filter(lambda x: isinstance(x, int), li)) print(res) Output15 Explanation:filter(lambda x: isinstance(x, int), li): This applies a condition to each element of the list li .lambda x: isinstance(x, int): This part checks whether each element x is of type int.filter(): This returns a filtered iterator containing only the elements that satisfy the condition i.e. the integers from the list [1, 2, 3, 4, 5].sum() :This takes the filtered iterator and calculates the sum of the elements. In this case, it adds the integers 1 + 2 + 3 + 4 + 5, resulting in 15.Table of ContentUsing list comprehensionUsing for loop Using list comprehensionlist comprehension is a powerful way to create new lists by applying an expression to each element in an existing list. When combined with the sum() function, it provides an efficient method for filtering and summing only the numeric values from a list that contains mixed data types. Python li = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] res = sum([x for x in li if isinstance(x, int)]) print(res) Output15 Explanation:list comprehension : This filters the list li to include only integer values and the condition isinstance(x, int) checks if each element x is an integer.sum() : This function then adds the values in the filtered list .Using for loop for loop is a simple way to iterate through a list and sum numeric values. While easy to understand, it is less efficient than methods like filter() and list comprehension as it requires manually checking each element and summing conditionally. Python li = [1, 2, 3, 4, 'a', 'b', 'x', 5, 'z'] # Manually summing numeric values using a loop res = 0 for item in li: if isinstance(item, int): res += item print(res) Output15 Explanation:For Loop: This iterates through each item in the list li.isinstance(item, int): This checks if the current item is an integer.res += item: This adds the integer values to res in each iteration. Comment More info G garg_ak0109 Follow Improve Article Tags : Python Python Programs python-list Python list-programs Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 5 min read Recursion in Python 6 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 5 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like