Skip to content
Related Articles

Related Articles

Python – Dual Tuple Alternate summation

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Jul, 2022
View Discussion
Improve Article
Save Article

Given dual tuple, perform summation of alternate elements, i.e of indices alternatively.

Input : test_list = [(4, 1), (5, 6), (3, 5), (7, 5)] 
Output : 18 
Explanation : 4 + 6 + 3 + 5 = 18, Alternatively are added to sum. 

Input : test_list = [(4, 1), (5, 6), (3, 5)] 
Output : 13 
Explanation : 4 + 6 + 3 = 13, Alternatively are added to sum. 

Method #1 : Using loop

In this, we iterate through each tuple, and check for index in list, if even, 1st element of tuple is summed else 2nd is added to be computed sum.

Python3




# Python3 code to demonstrate working of
# Dual Tuple Alternate summation
# Using loop
 
# initializing list
test_list = [(4, 1), (5, 6), (3, 5), (7, 5), (1, 10)]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = 0
for idx in range(len(test_list)):
 
    # checking for Alternate element
    if idx % 2 == 0:
        res += test_list[idx][0]
    else:
        res += test_list[idx][1]
 
# printing result
print("Summation of Alternate elements of tuples : " + str(res))

Output

The original list is : [(4, 1), (5, 6), (3, 5), (7, 5), (1, 10)]
Summation of Alternate elements of tuples : 19

Method #2 : Using list comprehension + sum()

In this, we perform task of getting summation using sum(), and list comprehension is used to provide compact solution for iteration of tuples in list.

Python3




# Python3 code to demonstrate working of
# Dual Tuple Alternate summation
# Using list comprehension + sum()
 
# initializing list
test_list = [(4, 1), (5, 6), (3, 5), (7, 5), (1, 10)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# summation using sum(), list comprehension offers shorthand
res = sum([test_list[idx][0] if idx % 2 == 0 else test_list[idx][1] for idx in range(len(test_list))])
 
# printing result
print("Summation of Alternate elements of tuples : " + str(res))

Output

The original list is : [(4, 1), (5, 6), (3, 5), (7, 5), (1, 10)]
Summation of Alternate elements of tuples : 19

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!