The filter() and Lambda Functions
The filter() function in Python can be used to construct an iterable from elements of another iterable that satisfy a specific condition. When combined with a lambda expression, it provides a clean way to filter out numbers based on divisibility. The list() function is used to convert the result back into a list.
def find_divisibles(myList, num):
# filter() and lambda functions
return list(filter(lambda i: i % num == 0, myList))
myList = [8, 14, 21, 36, 43, 57, 63, 71, 83, 93]
num = 3
print(find_divisibles(myList, num))
Output:
[21, 36, 57, 63, 93]
Python Program to Find Numbers Divisible by Another Number
We are given a list of numbers and a number. We have to find all the numbers in the list that are divisible by the given single number.
Examples:
Input: list=[8, 14, 21, 36, 43], num=3
Output: 21, 36, 57
Input: list=[2, 17, 25, 31, 48, 55], num=5
Output: 25, 55
In this article, we will discuss the different ways to find numbers divisible by another number in Python.