Skip to content
Related Articles

Related Articles

Python | Element indices Summation

View Discussion
Improve Article
Save Article
  • Last Updated : 02 Aug, 2022
View Discussion
Improve Article
Save Article

Usually, we require to find the sum of index, in which the particular value is located. There are many method to achieve that, using index() etc. But sometimes require to find all the indices of a particular value in case it has multiple occurrences in list. Lets discuss certain ways to do so.

Method #1 : Naive Method + sum() We can achieve this task by iterating through the list and check for that value and just append the value index in new list and print that. This is the basic brute force method to achieve this task. The sum() is used to perform sum of list. 

Python3




# Python code to demonstrate
# Element indices Summation
# using naive method + sum()
 
# initializing list
test_list = [1, 3, 4, 3, 6, 7]
 
# printing initial list
print ("Original list : " + str(test_list))
 
# using naive method
# Element indices Summation
# to find indices for 3
res_list = []
for i in range(0, len(test_list)) :
    if test_list[i] == 3 :
        res_list.append(i)
res = sum(res_list)
 
# printing resultant list
print ("New indices summation : " + str(res))

Output : 

Original list : [1, 3, 4, 3, 6, 7]
New indices summation : 4

  Method #2 : Using list comprehension + sum() List comprehension is just the shorthand technique to achieve the brute force task, just uses lesser lines of codes to achieve the task and hence saves programmers time. The sum() is used to perform sum of list. 

Python3




# Python code to demonstrate
# Element indices Summation
# using list comprehension + sum()
 
# initializing list
test_list = [1, 3, 4, 3, 6, 7]
 
# printing initial list
print ("Original list : " + str(test_list))
 
# using list comprehension + sum()
# Element indices Summation
# to find indices for 3
res_list = [i for i in range(len(test_list)) if test_list[i] == 3]
res = sum(res_list)
 
# printing resultant list
print ("New indices summation : " + str(res))

Output : 

Original list : [1, 3, 4, 3, 6, 7]
New indices summation : 4

Method#3 : Using reduce and lambda function With these function we can perform this task. We can use reduce to iterate over the list range and with lambda function checks if element is define value or not if then we add result with index of element else leave result. 

Python3




# Python code to demonstrate
# Element indices Summation
# using reduce + lambda function
from  functools import reduce
# initializing list
Tlist = [1, 3, 4, 3, 6, 7]
 
# printing initial list
print ("Original list : " + str(Tlist))
 
# using reduce + lambda
# Element indices Summation
# to find indices for 3
rang = len(Tlist)
res = reduce(lambda x, y : x + y if (Tlist[y] == 3) else x, range(rang), 0 )
 
# printing resultant list
print ("New indices summation : " + str(res))

Output:

Original list : [1, 3, 4, 3, 6, 7]
New indices summation : 4

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!