Python program to print elements which are multiples of elements given in a list

PythonServer Side ProgrammingProgramming

When it is required to print the elements which are multiples of elements given in a list, a list comprehension is used.

Example

Below is a demonstration of the same

my_list = [45, 67, 89, 90, 10, 98, 10, 12, 23]

print("The list is :")
print(my_list)

my_division_list = [6, 4]
print("The division list is :")
print(my_division_list)

my_result = [element for element in my_list if all(element % j == 0 for j in my_division_list)]

print("The result is :")
print(my_result)

Output

The list is :
[45, 67, 89, 90, 10, 98, 10, 12, 23]
The division list is :
[6, 4]
The result is :
[12]

Explanation

  • A list is defined and is displayed on the console.

  • Another list of integers is defined.

  • A list comprehension is used to iterate over the elements and check if the element divided by element in the integer list gives a remainder 0.

  • If yes, it is stored in a list and assigned to a variable.

  • This is displayed as output on the console.

raja
Published on 16-Sep-2021 08:31:21
Advertisements