Python – Find the sum of Length of Strings at given indices
Last Updated :
26 Apr, 2023
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
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
idx_list = [ 0 , 1 , 4 ]
res = 0
for idx, ele in enumerate (test_list):
if idx in idx_list:
res + = len (ele)
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
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
idx_list = [ 0 , 1 , 4 ]
res = sum ([ len (ele) for idx, ele in enumerate (test_list) if idx in idx_list])
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
- Initialize test_list with the list of strings [“gfg”, “is”, “best”, “for”, “geeks”].
- Initialize idx_list with the list of indices [0, 1, 4].
- Convert idx_list to a set using set(idx_list). This will make it easier to check if an index is in idx_list.
- Loop through the indices i of test_list using for i in range(len(test_list)).
- 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.
- 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].
- Once all matching elements have been collected, use sum(len(ele) for ele in matching_elements) to compute the sum of their lengths.
- Assign the result to res.
- Print the result using print(“Computed Strings lengths sum : ” + str(res)).
Python3
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
idx_list = [ 0 , 1 , 4 ]
idx_set = set (idx_list)
matching_elements = [test_list[i] for i in range ( len (test_list)) if i in idx_set]
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
Share your thoughts in the comments
Please Login to comment...