Python – Consecutive Division in List

PythonServer Side ProgrammingProgramming

When it is required to find the consecutive division in a list, a method is defined that iterates over the elements of the list and uses the ‘/’ operator to determine the result.

Below is a demonstration of the same −

Example

 Live Demo

def consec_division(my_list):

   my_result = my_list[0]
   for idx in range(1, len(my_list)):

      my_result /= my_list[idx]
   return my_result

my_list = [2200, 500, 100, 50, 20, 5]

print("The list is :")
print(my_list)

my_result = consec_division(my_list)

print("The result is :")
print(my_result)

Output

The list is :
[2200, 500, 100, 50, 20, 5]
The result is :
8.8e-06

Explanation

  • A method named ‘consec_division’ is defined that takes a list as a parameter.

  • It assigns the zeroth index to a variable.

  • The list is iterated over, and the ‘/’ operator is used to divide every element by the first element.

  • This is returned as result.

  • Outside the method, a list is defined and is displayed on the console.

  • The method is called by passing the list.

  • This is assigned to a variable.

  • It is displayed as output on the console.

raja
Published on 04-Sep-2021 11:10:54
Advertisements