In this example, you will learn how to find numbers divisible by another using different examples.
# Define a list of numbers
numbers = [12, 27, 45, 22, 9, 15, 36, 18, 30, 7]
# Technique 1: Using filter() and lambda
divisible_by_3 = list(filter(lambda x: x % 3 == 0, numbers))
print(divisible_by_3)
# Technique 2: Using a list comprehension
divisible_by_3 = [x for x in numbers if x % 3 == 0]
print(divisible_by_3)
# Technique 3: Using a loop and an empty list
divisible_by_3 = []
for x in numbers:
if x % 3 == 0:
divisible_by_3.append(x)
print(divisible_by_3)
Output
The output of the program is shown below