Python - Get summation of numbers in string list
Last Updated :
17 Apr, 2023
Sometimes, while working with data, we can have a problem in which we receive series of lists with data in string format, which we wish to accumulate as list. Let's discuss certain ways in which this task can be performed.
Method #1 : Using loop + int() This is the brute force method to perform this task. In this, we run a loop for entire list, convert each string to integer and perform summation listwise and store in a separate list.
Python3
# Python3 code to demonstrate working of
# Summation of String Integer lists
# using loop + int()
# initialize list
test_list = [['1', '4'], ['5', '6'], ['7', '10']]
# printing original list
print("The original list : " + str(test_list))
# Summation of String Integer lists
# using loop + int()
res = []
for sub in test_list:
par_sum = 0
for ele in sub:
par_sum = par_sum + int(ele)
res.append(par_sum)
# printing result
print("List after summation of nested string lists : " + str(res))
Output : The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after summation of nested string lists : [5, 11, 17]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using loop + int() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using sum() + int() + list comprehension This is the shorthand with the help of which this task can be performed. In this, we run a loop on lists using list comprehension and extract summation using sum().
Python3
# Python3 code to demonstrate working of
# Summation of String Integer lists
# using sum() + int() + list comprehension
# initialize list
test_list = [['1', '4'], ['5', '6'], ['7', '10']]
# printing original list
print("The original list : " + str(test_list))
# Summation of String Integer lists
# using sum() + int() + list comprehension
res = [sum(int(ele) for ele in sub) for sub in test_list]
# printing result
print("List after summation of nested string lists : " + str(res))
Output : The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after summation of nested string lists : [5, 11, 17]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3 : Using map() and lambda function
Python3
#Python3 code to demonstrate working of
#Summation of String Integer lists
#using map() and lambda function
#initialize list
test_list = [['1', '4'], ['5', '6'], ['7', '10']]
#printing original list
print("The original list : " + str(test_list))
#Summation of String Integer lists
#using map() and lambda function
res = list(map(lambda sub: sum(map(int, sub)), test_list))
#printing result
print("List after summation of nested string lists : " + str(res))
#this code is contributed by edula vinay kumar reddy
OutputThe original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after summation of nested string lists : [5, 11, 17]
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python | Embedded Numbers Summation in String List Sometimes, while working with Python lists, we can have problem in which we need to concatenate embedded numbers in Strings list and perform its summation. This can have application in domains dealing with data. Lets discuss certain ways in which this task can be performed. Method #1 : Using join()
6 min read
Python - Summation of float string list Sometimes, while working with Python list, we can have a problem in which we need to find summation in list. But sometimes, we donât have a natural number but a floating-point number in string format. This problem can occur while working with data, both in web development and Data Science domain. Le
7 min read
Python - Extract numbers from list of strings We are given a list of string we need to extract numbers from the list of string. For example we are given a list of strings s = ['apple 123', 'banana 456', 'cherry 789'] we need to extract numbers from the string in list so that output becomes [123, 456, 789].Using Regular ExpressionsThis method us
2 min read
Python - Maximum Pair Summation in numeric String Sometimes, we might have a problem in which we require to get the maximum summation of 2 numbers from Strings but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Letâs discuss certain ways in which this problem can be solved. Meth
6 min read
Sum of number digits in List in Python Our goal is to calculate the sum of digits for each number in a list in Python. This can be done by iterating through each number, converting it to a string, and summing its digits individually. We can achieve this using Pythonâs built-in functions like sum(), map(), and list comprehensions. For exa
2 min read
Python | Convert Stream of numbers to list Sometimes, we can be stuck with a problem in which we are given a stream of space separated numbers with a goal to convert them into a list of numbers. This type of problem can occur in common day-day programming or competitive programming while taking inputs. Let's discuss certain ways in which thi
5 min read
Python - Sort list of numbers by sum of their digits Sorting a list of numbers by the sum of their digits involves ordering the numbers based on the sum of each individual digit within the number. This approach helps prioritize numbers with smaller or larger digit sums, depending on the use case.Using sorted() with a Lambda Functionsorted() function w
2 min read
Python | Strings length summation Sometimes we receive data in the container that we need to process to handle it further for some essential utility. The magnitude of amount of data sometimes becomes important and needs to be known. This article discusses the total length of list of strings. Let's discuss certain ways in which this
5 min read
Frequency of Numbers in String - Python We are given a string and we have to determine how many numeric characters (digits) are present in the given string. For example: "Hello123World456" has 6 numeric characters (1, 2, 3, 4, 5, 6).Using re.findall() re.findall() function from the re module is a powerful tool that can be used to match sp
3 min read
Print all Strong Numbers in Given List - Python The task of printing all Strong numbers from a given list in Python involves iterating through the list and checking each number based on its digit factorial sum. A Strong number is a number whose sum of the factorials of its digits equals the number itself. For example, given a list a = [145, 375,
3 min read