- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Consecutive Division in List
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
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.
Advertisements