Python | Alternate front – rear Sum

While working with python, we usually come by many problems that we need to solve in day-day and in development. Specially in development, small tasks of python are desired to be performed in just one line. We discuss some ways to compute a list consisting of elements that are alternate front-rear sum in the list.

Method #1 : Using loop
This is brute force method in which this problem can be solved. In this, we take two pointers and store their sum in array while increasing and decreasing their positions.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Alternate front - rear Sum
# using loop
  
# initializing list 
test_list = [1, 4, 5, 3, 6, 7]
  
# printing original list 
print ("The original list is : " + str(test_list))
  
# Alternate front - rear Sum
# using loop
res = []
j = len(test_list) - 1
for i in range(0, len(test_list) // 2):
    res.append(test_list[i] + test_list[j])
    j = j - 1
  
# printing result
print ("The alterate front - rear Sum list is : " + str(res))

chevron_right


Output :

The original list is : [1, 4, 5, 3, 6, 7]
The alterate front - rear Sum list is : [8, 10, 8]

 

Method #2 : Using list comprehension
Naive method can be used to perform, but list comprehension provides a one liner method to perform this task.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Alternate front - rear Sum
# using list comprehension
  
# initializing list 
test_list = [1, 4, 5, 3, 6, 7]
  
# printing original list 
print ("The original list is : " + str(test_list))
  
# Alternate front - rear Sum
# using list comprehension
res = [test_list[i] + test_list[len(test_list) - (i + 1)] for i in range(len(test_list) // 2)]
  
# printing result
print ("The alterate front - rear Sum list is : " + str(res))

chevron_right


Output :

The original list is : [1, 4, 5, 3, 6, 7]
The alterate front - rear Sum list is : [8, 10, 8]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.