Sum of list (with string types) in Python
Last Updated :
11 Jul, 2025
In Python, type of behavior will not change for data type. Below example is list containing integer type in string.so, we have to take all int type numbers from list even it is declared in string.
Examples:
Input : list1 = [12, 'geek', 2, '41', 'for', 10, '8', 6, 4, 'geeks', 7, '10']
Output : 100
Input : list1 = [100, 'geek', 200, '400', 'for', 101, '2018',
64, 74, 'geeks', 27, '7810']
Output :10794
We use type() in Python and isdigit() in Python to achieve this.
Python3
# Python program to find sum of list with different
# types.
def calsum(l):
# returning sum of list using List comprehension
return sum([int(i) for i in l if type(i)== int or i.isdigit()])
# Declaring list
list1 = [12, 'geek', 2, '41', 'for', 10, '8', 6, 4, 'geeks', 7, '10']
list2 = [100, 'geek', 200, '400', 'for', 101, '2018', 64, 74, 'geeks', 27, '7810']
# Result of sum of list
print (calsum(list1))
print (calsum(list2))
Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(1)
An alternative approach to sum a list with string elements in Python is to use the try and except statements to handle the case when a string element cannot be converted to an integer. This approach can be implemented as follows:
Python3
def sum_list(l):
total = 0
for i in l:
try:
total += int(i)
except ValueError:
# ignore non-integer elements
pass
return total
list1 = [12, 'geek', 2, '41', 'for', 10, '8', 6, 4, 'geeks', 7, '10']
list2 = [100, 'geek', 200, '400', 'for', 101, '2018', 64, 74, 'geeks', 27, '7810']
print(sum_list(list1))
print(sum_list(list2))
This implementation uses a for loop to iterate over the elements of the list, and uses the try and except statements to catch the ValueError exception that is raised when a string element cannot be converted to an integer. When this exception is encountered, the except block is executed, which simply ignores the element and continues to the next iteration of the loop. This allows the sum_list function to sum only the integer elements of the list, while ignoring the non-integer.
The time complexity of the sum_list function is O(n), where n is the length of the list. This is because the function has to iterate over all the elements of the list in order to process them.
The auxiliary space complexity of the sum_list function is also O(1), as it uses a single variable total to store the sum of the elements in the list.
Method : Using isinstance() and isdigit() methods
Python3
# Python program to find sum of list with different
# types.
def calsum(l):
s=0
for i in l:
if isinstance(i,int) or i.isdigit():
s+=int(i)
return s
# Declaring list
list1 = [12, 'geek', 2, '41', 'for', 10, '8', 6, 4, 'geeks', 7, '10']
list2 = [100, 'geek', 200, '400', 'for', 101, '2018', 64, 74, 'geeks', 27, '7810']
# Result of sum of list
print (calsum(list1))
print (calsum(list2))
Time Complexity : O(N)
Auxiliary Space : O(1)
METHOD 4:Using filter, lambda function, and map
APPROACH:
The given code snippet calculates the sum of all numeric values in the given list, which contains elements of both integer and string types. It uses a lambda function to convert each numeric string to an integer, and a filter function to exclude non-numeric elements from the list. Then, the sum() function is used to compute the sum of the resulting list of integers.
ALGORITHM:
1. Create a list of elements to be processed.
2. Use the filter() function with a lambda function to filter out the non-numeric elements from the list.
3. Use the map() function with a lambda function to convert the numeric string elements to integers.
4. Use the sum() function to compute the sum of the resulting list of integers.
5. Store the computed sum in the variable 'sum'.
Python3
list1 = [12, 'geek', 2, '41', 'for', 10, '8', 6, 4, 'geeks', 7, '10']
sum = sum(map(lambda x: int(x), filter(lambda x: str(x).isdigit(), list1)))
print(sum)
The time complexity of this code is O(n), where n is the length of the input list.
The space complexity of this code is also O(n), where n is the length of the input list.
Similar Reads
Position Summation in List of Tuples - Python Position Summation in List of Tuples refers to the process of calculating the sum of elements at the same positions across multiple tuples in a list. This operation involves adding up the corresponding elements from each tuple.For example, consider the list of tuples [(1, 6), (3, 4), (5, 8)]. The go
3 min read
Python | Set 3 (Strings, Lists, Tuples, Iterations) In the previous article, we read about the basics of Python. Now, we continue with some more python concepts. Strings in Python: A string is a sequence of characters that can be a combination of letters, numbers, and special characters. It can be declared in python by using single quotes, double quo
3 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Output of python program | Set 12(Lists and Tuples) Prerequisite: List and Tuples Note: Output of all these programs is tested on Python3 1) What is the output of the following program? PYTHON L1 = [] L1.append([1, [2, 3], 4]) L1.extend([7, 8, 9]) print(L1[0][1][1] + L1[2]) a) Type Error: can only concatenate list (not "int") to list b) 12 c) 11 d) 3
3 min read
Convert a List of Characters into a String - Python Our task is to convert a list of characters into a single string. For example, if the input is ['H', 'e', 'l', 'l', 'o'], the output should be "Hello".Using join() We can convert a list of characters into a string using join() method, this method concatenates the list elements (which should be strin
2 min read
How To Convert Data Types in Python 3? Type Conversion is also known as typecasting, is an important feature in Python that allows developers to convert a variable of one type into another. In Python 3, type conversion can be done both explicitly (manual conversion) and implicitly (automatic conversion by Python).Table of ContentTypes of
4 min read