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, 'Lazyroar', 7, '10'] Output : 100 Input : list1 = [100, 'geek', 200, '400', 'for', 101, '2018', 64, 74, 'Lazyroar', 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 , 'Lazyroar' , 7 , '10' ] list2 = [ 100 , 'geek' , 200 , '400' , 'for' , 101 , '2018' , 64 , 74 , 'Lazyroar' , 27 , '7810' ] # Result of sum of list print (calsum(list1)) print (calsum(list2)) |
100 10794
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 , 'Lazyroar' , 7 , '10' ] list2 = [ 100 , 'geek' , 200 , '400' , 'for' , 101 , '2018' , 64 , 74 , 'Lazyroar' , 27 , '7810' ] print (sum_list(list1)) print (sum_list(list2)) |
100 10794
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 , 'Lazyroar' , 7 , '10' ] list2 = [ 100 , 'geek' , 200 , '400' , 'for' , 101 , '2018' , 64 , 74 , 'Lazyroar' , 27 , '7810' ] # Result of sum of list print (calsum(list1)) print (calsum(list2)) |
100 10794
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 , 'Lazyroar' , 7 , '10' ] sum = sum ( map ( lambda x: int (x), filter ( lambda x: str (x).isdigit(), list1))) print ( sum ) |
100
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.