Tuesday, November 19, 2024
Google search engine
HomeLanguagesPython | Strings length summation

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 can be done. 

Method #1 : Using sum() + list comprehension The combination of these two functions can be used to perform this particular function. The sum function is used to find the summation of each string of list and list comprehension does the task of iteration. 

Python3




# Python3 code to demonstrate
# string lengths summation
# using sum() + list comprehension
 
# initializing list of tuples
test_list = ['Geeks', 'for', 'Geeks']
 
# printing the original list
print ("The original list is : " + str(test_list))
 
# using sum() + list comprehension
# string lengths summation
res = sum(len(i) for i in test_list)
 
# printing result
print ("The summation of strings is : " + str(res))


Output:

The original list is : ['Geeks', 'for', 'Geeks']
The summation of strings is : 13

Time Complexity: O(n) where n is the number of elements in the test_list. The sum() + list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.

Method #2 : Using join() + len() The inbuilt functions of python can help to perform this particular task. The join function can be used to join all the strings together and len function takes their cumulative sum. 

Python3




# Python3 code to demonstrate
# string lengths summation
# using sum() + list comprehension
 
# initializing list of tuples
test_list = ['Geeks', 'for', 'Geeks']
 
# printing the original list
print ("The original list is : " + str(test_list))
 
# using sum() + list comprehension
# string lengths summation
res = len(''.join(test_list))
 
# printing result
print ("The summation of strings is : " + str(res))


Output:

The original list is : ['Geeks', 'for', 'Geeks']
The summation of strings is : 13

Time Complexity: O(n) where n is the number of elements in the test_list. The join() + len() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.

Method #3 : Using reduce(one-liner)  you could use the built-in reduce() function from the functools module to perform the summation. The reduce() function applies a function to a list of elements, reducing the list to a single value. In this case, the function could be a lambda function that returns the sum of the lengths of two strings. The following code demonstrates how to use reduce() to find the total length of a list of strings:

Python3




from functools import reduce
# Python3 code to demonstrate
# string lengths summation
   
# initializing list of tuples
test_list = ['Geeks', 'for', 'Geeks']
# initialize a total_length variable to 0
total_length = 0
 
# use reduce() to find the total length of the list of strings
total_length = reduce(lambda x, y: x + len(y), test_list, 0)
 
# print the total length
print(total_length)
#This code is contributed by Edula Vinay Kumar Reddy


Output

13

Time complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using map() with len()

The map() function in Python takes a function and applies it to every element of an iterable. In this case, we can use the len() function as the input function to map() and apply it to every string element of the list. We can then use the built-in sum() function to calculate the total length of all the strings.

Algorithm:

  • Initialize a list of strings.
  • Apply the map() function with len() as the input function to get a list of lengths of each string element.
  • Use sum() function to calculate the total length of all the strings.
  • Print the result.

Python3




# Python3 code to demonstrate
# string lengths summation
# using map() with len()
  
# initialize list of strings
test_list = ['Geeks', 'for', 'Geeks']
  
# printing the original list
print("The original list is : " + str(test_list))
  
# using map() with len()
# string lengths summation
lengths = list(map(len, test_list))
res = sum(lengths)
  
# printing result
print("The summation of strings is : " + str(res))


Output

The original list is : ['Geeks', 'for', 'Geeks']
The summation of strings is : 13

Time complexity: O(n), where n is the number of elements in the test_list. The map() function with len() as the input function takes O(n) time and the sum() function takes O(1) time.

Auxiliary Space: O(n), where n is the number of elements in the test_list. The map() function creates a list of length n and the sum() function uses a constant amount of extra space.

Method 5: Using a for loop.

Step-by-step approach:

  • Initialize a variable sum_length to 0 to store the sum of lengths of strings.
  • Use a for loop to iterate through the list of strings test_list.
  • For each string s in the list, add its length to sum_length.
  • Finally, print the sum of lengths.

Python3




# initialize list of strings
test_list = ['Geeks', 'for', 'Geeks']
 
# initialize variable to store sum of lengths
sum_length = 0
 
# iterate through the list of strings and add length of each string to sum_length
for s in test_list:
    sum_length += len(s)
 
# print the sum of lengths
print("The summation of strings is : " + str(sum_length))


Output

The summation of strings is : 13

Time complexity: O(n), where n is the number of strings in the list.
Auxiliary space: O(1), as we are using a single variable to store the sum of lengths.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments