When working with numbers in Python, you often need to filter divisible numbers by another number. In this tutorial, we’ll cover the various methods to find numbers divisible by another number.
Table of contents
1. How to find the Numbers Divisible by Another Number in Python
This is a simple approach to finding a divisible number in Python by checking each number in the list and seeing if it is divisible by the given divisor.
We are iterating over the list using a for loop and checking divisibility using the modulo operator.
The modulo operator (%) in Python returns the remainder after dividing one number by another. If the remainder is 0, then it’s divisible. Else, it is not.
Steps to find numbers divisible by another number:
- Get Input
A list (or any iterable) of numbers (let’s call it
numbers).
The divisor (let’s call itdivisor). - Initialize an empty list
Create an empty list called
divisible_numbersto store the numbers that are divisible by thedivisor. - Iterate through the
numberslistIterating over the number list using a for loop
- Check for divisibility
In each iteration use modulo operator (
%) to check If the current number is divisible bydivisor. (i.e., ifnumber % divisor == 0), then Append it to thedivisible_numberslist. - Return the result
Return the
divisible_numberslist.
Code Example
Output:
[10, 40, 50, 60]Code language: Python (python)
2. Using the filter() Function
Here, filter() function is used to find numbers divisible by a divisor using Python. The filter() function filters elements from an iterable based on a given condition (function). It returns only the elements that satisfy the condition.
Code Example
Explanation
- The
filter()useslambdato iterate over each item in the list of numbers and apply the conditionx % divisor == 0to each number. - If the number passes the condition, it collects it into the iterable and returns it with all the divisible numbers. (An iterable in Python is any object that can be looped over/iterated)
- The result is an iterator containing only the divisible numbers, which are then printed using
*by unpacking the elements in the iterator.
3. Using Recursion
Recursion is a programming technique where a function calls itself to solve smaller instances of a problem until a base condition is met.
Here, we recursively check the number in the list to see if it is divisible by the given number in Python.
For Example: numbers = [10, 15, 40] and divisor = 10, the recursion will work as follows:
- First call:
numbers = [10, 15, 40]anddivisor = 10-> Check10 % 10 == 0, then it’s divisible. - Second call:
numbers = [15, 40]anddivisor = 10-> Check15 % 10 != 0, then it’s not divisible. - Third call:
numbers = [40]anddivisor = 10-> Check40 % 10 == 0, then it’s divisible. - Fourth call:
numbers = []-> Base case reached → Recursion stops.
Code Example
Output:
Number 10 is divisible by 10 Number 15 is NOT divisible by 10 Number 22 is NOT divisible by 10 Number 33 is NOT divisible by 10 Number 40 is divisible by 10 Number 50 is divisible by 10 Number 60 is divisible by 10
Explanation
- In each recursion, we check the condition on the first element in the list, and then the function calls itself with the list’s subset containing numbers from position 1 to the end of the list.
- String slicing (
numbers[1:]) creates a sublist starting from index1to the end of thenumberslist, effectively excluding the first element. - Each time, it checks and prints the first element if it’s divisible by the divisor. It continues till the list gets empty.
4. Using List Comprehension
List comprehension is a concise way to create Python lists by applying an expression to each item in an iterable, with optional filtering using a condition.
List comprehension is used over iterables like a list of numbers, checking for the condition number % divisor == 0 on each number. If the condition is true, then it returns the number. This way, by iterating over the list, we are collecting all divisible numbers into the list based on the filter condition.
Code Example
Summary
These Python methods can be used to find the numbers divisible by a given number based on the use case and the performance requirements.
- For Loop: Simple and easy to understand.
- Filter Function: Functional programming approach.
- Recursion: Useful for specific problems, like breaking down large tasks.
- List Comprehension: Concise and Pythonic.

Leave a Reply