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
test_list = [ 1 , 4 , 5 , 6 , 7 ]
print ("The original list is : " + str (test_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
test_list = [ 1 , 4 , 5 , 6 , 7 ]
print ("The original list is : " + str (test_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
test_list = [ 1 , 4 , 5 , 6 , 7 ]
print ( "The original list is : " + str (test_list))
res = [index + value for index, value in enumerate (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)
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 ]
res = list ( map ( lambda x: x[ 0 ] + x[ 1 ], enumerate (test_list)))
print ( "The list index-value summation is : " + str (res))
|
Output
The 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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...