
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Dual Tuple Alternate summation
When it is required to perform dual tuple alternate summation, a simple iteration and the modulus operator are used.
Below is a demonstration of the same −
Example
my_list = [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] print("The list is :") print(my_list) my_result = 0 for index in range(len(my_list)): if index % 2 == 0: my_result += my_list[index][0] else: my_result += my_list[index][1] print("The result is :") print(my_result)
Output
The list is : [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] The result is : 225
Explanation
A list of tuple is defined and displayed on the console.
A variable is initialized to 0.
A list comprehension is used to iterate over the elements of the list, and modulus operator is used to check if remainder of every element divided with 2 is equal to 0.
If yes, the element in 0th index is added to the variable.
Otherwise, the element in first index is added to the variable.
This is the output that is displayed on the console.
- Related Articles
- Alternate element summation in list (Python)
- Python Grouped summation of tuple list¶
- Grouped summation of tuple list in Python
- Summation of list as tuple attribute in Python
- Python - Column summation of tuples
- Python – Summation of consecutive elements power
- Python Alternate repr() implementation
- Summation of tuples in list in Python
- Dual Priority Queues
- Scalar multiplication with Einstein summation convention in Python
- Tensor contraction with Einstein summation convention in Python
- Alternate range slicing in list (Python)
- Array axis summations with Einstein summation convention in Python
- Vector inner product with Einstein summation convention in Python
- Matrix Vector multiplication with Einstein summation convention in Python

Advertisements