Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Alternate element summation in list (Python)
The given task is to perform an alternate summation on a Python List. For example, adding the elements at even indices (0, 2, 4) or odd indices (1, 3, 5), depending on the requirement. This means we need to add every other element from the given list.
Let us see an input scenario −
ScenarioInput: [11,22,33,44,55] Output: 99 Explanation: Here, we are going to add the elements at the even indices by using the slicing [::2].
Using sum() with List Slicing
The alternate element summation in a list can be done by using list slicing along with the built-in sum() function. The sum() function calculates the sum of elements from the sliced list, where slicing is used to pick every alternate element.
Syntax
Following is the syntax of the Python sum() function −
sum(iterable, start)
Slicing for Alternate Elements
- numbers[::2] picks elements at the even indices (0, 2, 4.....) starting from index 0.
- numbers[1::2] picks elements at the odd indices (1, 3, 5....) starting from index 1.
Example: Sum of Elements at Even Indices
Let's look at the following example, where we are going to sum the elements at even indices ?
numbers = [11, 22, 33, 44, 55, 66]
alternate_numbers = numbers[::2]
result = sum(alternate_numbers)
print("Result:", result)
The output of the above program is as follows −
Result: 99
Example: Sum of Elements at Odd Indices
Consider another example, where we are going to sum the elements at odd indices −
numbers = [2, 4, 6, 8, 10, 12, 14]
result = sum(numbers[1::2])
print("Result:", result)
Following is the output of the above program −
Result: 24
Example: Sum of Elements from the End
In the following example, we are going to sum the alternate elements from the end of the list −
numbers = [3, 6, 9, 12, 15, 18]
result = sum(numbers[::-2])
print("Result:", result)
If we run the above program, it will generate the following output −
Result: 36
Using a For Loop
The alternate element summation in a list can be achieved by using a for loop with the range() function that starts at index 0 and skips every second element by specifying the step value 2 in the range() function.
Example
Following is the example where we use a for loop along with the range and add the alternate elements starting from index 0 ?
numbers = [4, 8, 12, 16, 20]
result = 0
for i in range(0, len(numbers), 2):
result += numbers[i]
print("Result:", result)
Following is the output of the above program −
Result: 36
Using List Comprehension
You can also use list comprehension with enumerate() to sum alternate elements based on index parity ?
numbers = [5, 10, 15, 20, 25, 30]
# Sum elements at even indices
even_sum = sum([value for index, value in enumerate(numbers) if index % 2 == 0])
# Sum elements at odd indices
odd_sum = sum([value for index, value in enumerate(numbers) if index % 2 == 1])
print("Even indices sum:", even_sum)
print("Odd indices sum:", odd_sum)
The output of the above program is −
Even indices sum: 45 Odd indices sum: 60
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| sum() + slicing | High | Fast | Simple alternate summation |
| For loop | Medium | Medium | Complex logic needed |
| List comprehension | Medium | Good | Conditional summation |
Conclusion
Use sum(numbers[::2]) for simple alternate element summation as it's the most readable and efficient. For more complex conditions, list comprehension with enumerate() provides flexibility.
