Skip to content
Related Articles

Related Articles

Python – Maximum consecutive elements percentage change
  • Last Updated : 08 Jun, 2020

Sometimes, while working with Python lists, we can have a problem in which we need to extract the maximum change of consecutive elements. This kind of problem can have application in domains such as Data Science. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [4, 6, 7]
Output : 50.0

Input : test_list = [7, 7, 7, 7]
Output : 0.0

Method #1 : Using loop + zip()
The combination of above functions can be used to solve this problem. In this, we combine elements with its successive element using zip(). The loop is used to perform computation in brute manner.




# Python3 code to demonstrate working of 
# Maximum consecutive elements percentage change
# Using zip() + loop
  
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Maximum consecutive elements percentage change
# Using zip() + loop
res = 0
for x, y in zip(test_list, test_list[1:]):
    res = max((abs(x - y) / x) * 100, res)
  
# printing result 
print("The maximum percentage change : " + str(res)) 
Output :



The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The maximum percentage change : 300.0

 

Method #2 : Using recursion + max()
This is yet another way in which this task can be performed. In this, instead of loop, we perform this task using recursion, tracking maximum at each call.




# Python3 code to demonstrate working of 
# Maximum consecutive elements percentage change
# Using zip() + loop
  
# helpr_fnc
def get_max_diff(test_list, curr_max = None):
    pot_max = (abs(test_list[1] - test_list[0]) / test_list[0]) * 100
    if curr_max :
        pot_max = max(curr_max, pot_max)
    if len(test_list) == 2:
        return pot_max
    return get_max_diff(test_list[1:], pot_max)
  
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Maximum consecutive elements percentage change
# Using zip() + loop
res = get_max_diff(test_list)
  
# printing result 
print("The maximum percentage change : " + str(res)) 
Output :
The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The maximum percentage change : 300.0

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :