Python | Summation of integers in heterogenous list
Sometimes, while working with Python, we can come across a problem in which we require to find the sum of list. This problem is easier to solve. But this can get complex in case we have mixture of data types to go along with it. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using Type casting + Exception Handling
We can employ a brute force method to type caste each element and catch the exception if any occurs. This can ensure that only integers are added to sum and hence can solve the problem.
# Python3 code to demonstrate working of # Summation of integers in heterogenous list # using type caste and exception handling # initializing list test_list = [5, 6, "gfg", 8, (5, 7), 'is', 9] # printing original list print("The original list is : " + str(test_list)) # Summation of integers in heterogenous list # using type caste and exception handling res = 0for ele in test_list: try: res += int(ele) except : pass # printing result print("Summation of integers in list : " + str(res)) |
The original list is : [5, 6, 'gfg', 8, (5, 7), 'is', 9] Summation of integers in list : 28
Method #2 : Using sum() + isinstance()
This problem can also be solved using the inbuilt function of sum() and it also supports the instance filter using isinstance() which can be feeded with integer and hence solve the problem.
# Python3 code to demonstrate working of # Summation of integers in heterogenous list # using sum() + isinstance() # initializing list test_list = [5, 6, "gfg", 8, (5, 7), 'is', 9] # printing original list print("The original list is : " + str(test_list)) # Summation of integers in heterogenous list # using sum() + isinstance() res = sum(filter(lambda i: isinstance(i, int), test_list)) # printing result print("Summation of integers in list : " + str(res)) |
The original list is : [5, 6, 'gfg', 8, (5, 7), 'is', 9] Summation of integers in list : 28
Recommended Posts:
- Python | Alternate element summation in list
- Python | Summation of dictionary list values
- Python | Summation of list as tuple attribute
- Python | Grouped summation of tuple list
- Python | Pair summation of list elements
- Python | Position Summation in List of Tuples
- Python | Convert list of numerical string to list of Integers
- Python | Converting all strings in list to integers
- Python | Convert number to list of integers
- Python | Adding K to each element in a list of integers
- Python | Program to print duplicates from a list of integers
- Python | Convert a list of multiple integers into a single integer
- Python | Reverse sequence of strictly increasing integers in a list
- Python | Counting sign change in list containing Positive and Negative Integers
- Python | Summation of Non-Zero groups
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



