Python - Index Value Summation List
Last Updated :
09 Apr, 2023
To access the elements of lists, there are various methods. But sometimes we may require to access the element along with the index on which it is found and compute its summation, and for that, we may need to employ different strategies. This article discusses some of those strategies.
Method 1: Naive method This is the most generic method that can be possibly employed to perform this task of accessing the index along with the value of the list elements. This is done using a loop. The task of performing sum is performed using external variable to add.
Python3
# Python 3 code to demonstrate
# Index Value Summation List
# using naive method
# initializing list
test_list = [1, 4, 5, 6, 7]
# Printing list
print ("The original list is : " + str(test_list))
# using naive method to
# Index Value Summation List
res = []
for i in range(len(test_list)):
res.append(i + test_list[i])
print ("The list index-value summation is : " + str(res))
Output : The original list is : [1, 4, 5, 6, 7]
The list index-value summation is : [1, 5, 7, 9, 11]
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method 2: Using list comprehension + sum() This method works in similar way as the above method but uses the list comprehension technique for the same, this reduces the possible lines of code to be written and hence saves time. The task of performing summation is done by sum().
Python3
# Python 3 code to demonstrate
# Index Value Summation List
# using list comprehension
# initializing list
test_list = [1, 4, 5, 6, 7]
# Printing list
print ("The original list is : " + str(test_list))
# using list comprehension to
# Index Value Summation List
res = [sum(list((i, test_list[i]))) for i in range(len(test_list))]
print ("The list index-value summation is : " + str(res))
Output : The original list is : [1, 4, 5, 6, 7]
The list index-value summation is : [1, 5, 7, 9, 11]
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method 3: Using Enumerate
This method uses the built-in enumerate() method which is used to loop over a list along with the index of the elements present in it.
Python3
# Python 3 code to demonstrate
# Index Value Summation List
# using enumerate
# initializing list
test_list = [1, 4, 5, 6, 7]
# Printing list
print ("The original list is : " + str(test_list))
# using enumerate to
# Index Value Summation List
res = [index + value for index, value in enumerate(test_list)]
print ("The list index-value summation is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list is : [1, 4, 5, 6, 7]
The list index-value summation is : [1, 5, 7, 9, 11]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4 : Using map() and lambda function:
- The enumerate() function is used to generate a sequence of tuples (index, value) for each element in test_list.
- The map() function is used to apply a lambda function to each tuple in the sequence.
- The lambda function adds the index and value of each tuple together, which creates a new sequence of integers.
- The list() function is used to convert the sequence of integers to a new list res.
- The print() function is used to display the resulting list res to the user.
Python3
test_list = [1, 4, 5, 6, 7]
# Using map() and lambda function to sum the index
# and value for each element in test_list
# The enumerate() function is used to get the index
# and value of each element in the list
res = list(map(lambda x: x[0]+x[1], enumerate(test_list)))
print("The list index-value summation is : " + str(res))
OutputThe list index-value summation is : [1, 5, 7, 9, 11]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), as we create a new list res that contains the same number of elements as the input list.
Similar Reads
Python | Summation of tuples in list Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let's discuss certain ways in which this problem can be solved. Metho
7 min read
Python | Summation of dictionary list values Sometimes, while working with Python dictionaries, we can have its values as lists. In this can, we can have a problem in that we just require the count of elements in those lists as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let's discuss c
6 min read
Python - List of dictionaries all values Summation Given a list of dictionaries, extract all the values summation. Input : test_list = [{"Apple" : 2, "Mango" : 2, "Grapes" : 2}, {"Apple" : 2, "Mango" : 2, "Grapes" : 2}] Output : 12 Explanation : 2 + 2 +...(6-times) = 12, sum of all values. Input : test_list = [{"Apple" : 3, "Mango" : 2, "Grapes" : 2
5 min read
Python | Chuncked summation every K value The prefix array is quite famous in the programming world. This article would discuss a variation of this scheme. To perform a chunked summation where the sum resets at every occurrence of the value K , we need to modify the summation logic. The idea is:Traverse the list.Keep adding to the cumulativ
3 min read
Python | Accumulative index summation in tuple list Sometimes, while working with data, we can have a problem in which we need to find accumulative summation of each index in tuples. This problem can have applications in web development and competitive programming domain. Let's discuss certain way in which this problem can be solved. Method 1: Using
8 min read
Python | Grouped summation of tuple list Many times, we are given a list of tuples and we need to group its keys and perform certain operations while grouping. The most common operation is addition. Let's discuss certain ways in which this task can be performed. Apart from addition, other operations can also be performed by doing small cha
10 min read