In this Python lesson, we will practice how to write a program to find the numbers divisible by another number. Our aim is to find numbers divisible by 4 and we will achieve this purpose using the anonymous (lambda) function inside the filter() built-in function.
Example: Find Numbers Divisible by Another Number
# create list of numbers
div_nums = [6, 8, 12, 15, 17, 20, 25, 30, 45, 56]
# filter using anonymous function
result = list(filter(lambda x: (x % 4 == 0), div_nums))
# display the result
print('List of numbers divisible by 4:',result)
Output
List of numbers divisible by 4: [8, 12, 20, 56]
In the above program, we check for numbers divisible by 4 in the list and displayed them on the console. You can also check for numbers divisible by any other numbers by changing the value of 4 below to any number of your choice. Remember to also change the list values to your own choice too.
result = list(filter(lambda x: (x % 4 == 0), div_nums))