Open In App

Python – Find the sum of Length of Strings at given indices

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given the String list, write a Python program to compute sum of lengths of custom indices of list.

Examples:

Input : test_list = [“gfg”, “is”, “best”, “for”, “geeks”], idx_list = [0, 1, 4] 
Output : 10 
Explanation : 3 + 2 + 5 = 10. (Sizes of strings at idx.)
 

Input : test_list = [“gfg”, “is”, “best”, “for”, “geeks”], idx_list = [0, 2, 4] 
Output : 12 
Explanation : 3 + 4 + 5 = 12. 

Method #1 : Using len() + loop 

In this, we iterate for all indices and check if they occur in index list, if yes, increment frequency in summation counter.

Python3




# Python3 code to demonstrate working of
# Length sum of custom indices Strings
# Using len() + loop
 
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks"]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing idx list
idx_list = [0, 1, 4]
 
res = 0
for idx, ele in enumerate(test_list):
 
    # adding length if index in idx_list
    if idx in idx_list:
        res += len(ele)
 
# printing result
print("Computed Strings lengths sum : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Computed Strings lengths sum : 10

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

Method #2 : Using sum() + len() + list comprehension

In this, we perform task of performing summation using sum(), rest all the functionalities are performed as per above method, just as one-liner.

Python3




# Python3 code to demonstrate working of
# Length sum of custom indices Strings
# Using sum() + len() + list comprehension
 
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks"]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing idx list
idx_list = [0, 1, 4]
 
# performing summation using sum()
# len() used to get strings lengths
res = sum([len(ele) for idx, ele in enumerate(test_list) if idx in idx_list])
 
# printing result
print("Computed Strings lengths sum : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Computed Strings lengths sum : 10

The time and space complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)

Approach 3: Using map and lambda function

We can use the map function to apply the length of each element to the given indices.

Python3




test_list = ["gfg", "is", "best", "for", "geeks"]
idx_list = [0, 1, 4]
 
res = sum(list(map(lambda x: len(test_list[x]), idx_list)))
print("Computed Strings lengths sum :", res)


Output

Computed Strings lengths sum : 10

This approach also has a time complexity of O(n) and an auxiliary space of O(n).

Approach 5: Using the intersection of sets

  1. Initialize test_list with the list of strings [“gfg”, “is”, “best”, “for”, “geeks”].
  2. Initialize idx_list with the list of indices [0, 1, 4].
  3. Convert idx_list to a set using set(idx_list). This will make it easier to check if an index is in idx_list.
  4. Loop through the indices i of test_list using for i in range(len(test_list)).
  5. Check if i is in idx_set using if i in idx_set. This will determine if the current element in test_list has a matching index in idx_list.
  6. If i is in idx_set, append the element test_list[i] to matching_elements using [test_list[i] for i in range(len(test_list)) if i in idx_set].
  7. Once all matching elements have been collected, use sum(len(ele) for ele in matching_elements) to compute the sum of their lengths.
  8. Assign the result to res.
  9. Print the result using print(“Computed Strings lengths sum : ” + str(res)).

Python3




test_list = ["gfg", "is", "best", "for", "geeks"]
idx_list = [0, 1, 4]
 
# convert the index list to a set
idx_set = set(idx_list)
 
# use the intersection of sets to get the elements with matching indices
matching_elements = [test_list[i] for i in range(len(test_list)) if i in idx_set]
 
# sum the lengths of the matching elements
res = sum(len(ele) for ele in matching_elements)
 
print("Computed Strings lengths sum : " + str(res))


Output

Computed Strings lengths sum : 10

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(k), where k is the number of indices in the idx_list that match indices in the test_list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads