 Open in App
Not now

# Python | Summation of integers in heterogeneous list

• Last Updated : 20 Feb, 2023

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

 `# Python3 code to demonstrate working of``# Summation of integers in heterogeneous 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 heterogeneous list``# using type caste and exception handling``res ``=` `0``for` `ele ``in` `test_list:``    ``try``:``        ``res ``+``=` `int``(ele)``    ``except` `:``        ``pass` `# printing result``print``(``"Summation of integers in list : "` `+` `str``(res))`

Output :

```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

 `# Python3 code to demonstrate working of``# Summation of integers in heterogeneous 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 heterogeneous list``# using sum() + isinstance()``res ``=` `sum``(``filter``(``lambda` `i: ``isinstance``(i, ``int``), test_list))` `# printing result``print``(``"Summation of integers in list : "` `+` `str``(res))`

Output :

```The original list is : [5, 6, 'gfg', 8, (5, 7), 'is', 9]
Summation of integers in list : 28```

Method #3 : Using type() method

## Python3

 `# Python3 code to demonstrate working of``# Summation of integers in heterogeneous list` `# 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 heterogeneous list``# using type caste and exception handling``res ``=` `0``for` `ele ``in` `test_list:``    ``if` `type``(ele) ``is` `int``:``        ``res``+``=``ele` `# printing result``print``(``"Summation of integers in list : "` `+` `str``(res))`

Output

```The original list is : [5, 6, 'gfg', 8, (5, 7), 'is', 9]
Summation of integers in list : 28```

Time Complexity : O(N)
Auxiliary Space : O(N)

Method 4: Using for loop

We first initialize the list and print it. Then, we define a variable res to store the sum of integers in the list. We use a loop to iterate over each element of the list and check if it is an integer using the isinstance() function. If it is an integer, we add it to res. Finally, we print the sum of integers in the list.

Below is the implementation:

## Python3

 `# Python3 code to demonstrate working of``# Summation of integers in heterogeneous list``# using loop` `# 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 heterogeneous list``# using loop``res ``=` `0``for` `i ``in` `test_list:``    ``if` `isinstance``(i, ``int``):``        ``res ``+``=` `i` `# printing result``print``(``"Summation of integers in list : "` `+` `str``(res))`

Output

```The original list is : [5, 6, 'gfg', 8, (5, 7), 'is', 9]
Summation of integers in list : 28```

Time complexity: O(n), where n is the length of the input list. This is because the loop iterates over each element of the list once, and the time taken for each iteration is constant.
Auxiliary space: O(1), which is constant.

My Personal Notes arrow_drop_up