Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list, the task here is to write a Python program to extract elements which are multiple of all elements of custom list.

Input : test_list = [4, 24, 8, 10, 12, 23], div_list = [6, 4] 
Output : [24, 12] 
Explanation : 24 and 12 divides 6 and 4 both.

Input : test_list = [4, 24, 8, 10, 12, 23], div_list = [6, 4, 7] 
Output : [] 
Explanation : No elements divides 6, 4 and 7. 

Method 1: Using list comprehension and all()

In this, we perform task of checking for all elements to be multiple using % operator and all(). List comprehension is used to iterate through all the elements.

Python3




# initializing List
test_list = [4, 24, 8, 10, 12, 23]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing divisor list
div_list = [6, 4]
 
# using all() to test for all elements
res = [ele for ele in test_list if all(ele % j == 0 for j in div_list)]
 
# printing result
print("All elements multiple of divisor list : " + str(res))


Output:

The original list is : [4, 24, 8, 10, 12, 23]

All elements multiple of divisor list : [24, 12]

Time Complexity: O(n)
Auxiliary Space: O(1)

Method 2: Using filter(), lambda and all()

In this, we perform task of filtering using filter() and lambda, rest all the operations are performed like above method.

Python3




# initializing List
test_list = [4, 24, 8, 10, 12, 23]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing divisor list
div_list = [6, 4]
 
# using all() to test for all elements
# using filter() and lambda to perform filtering
res = list(filter(lambda ele: all(ele % j == 0 for j in div_list), test_list))
 
# printing result
print("All elements multiple of divisor list : " + str(res))


Output:

The original list is : [4, 24, 8, 10, 12, 23]

All elements multiple of divisor list : [24, 12]

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method 3: Uses a loop

Step-by-step approach:

  • Initialize the input list and the divisor list.
  • Initialize an empty list to store the result.
  • Loop through each element in the test_list.
  • For each element, loop through each divisor in the div_list.
  • Check if the current element is divisible by the current divisor. If not, set the flag variable to False and break out of the loop.
  • If the flag variable is True after checking all the divisors, append the current element to the result list.
  • Print the result.

Python3




# initializing List
test_list = [4, 24, 8, 10, 12, 23]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing divisor list
div_list = [6, 4]
 
# initializing an empty list to store the results
res = []
 
# looping through each element in the test_list
for ele in test_list:
    # initializing a flag variable to keep track of whether the current element is divisible by all the divisors in div_list
    flag = True
    # looping through each divisor in div_list
    for div in div_list:
        # if the current element is not divisible by any of the divisors in div_list, set the flag to False and break out of the loop
        if ele % div != 0:
            flag = False
            break
    # if the flag is True, append the current element to the result list
    if flag:
        res.append(ele)
 
# printing result
print("All elements multiple of divisor list : " + str(res))


Output

The original list is : [4, 24, 8, 10, 12, 23]
All elements multiple of divisor list : [24, 12]

Time complexity: O(n*m), where n is the length of test_list and m is the length of div_list.
Auxiliary space: O(k), where k is the number of elements in the result list.

Method 4: Use the set intersection operation. 

Step-by-step approach:

  • Convert the div_list to a set using the set() function.
  • Create a set comprehension to get a set of all the elements in the test_list that are divisible by all the elements in the div_list set. This can be done using the all() function and the modulus operator (%).
  • Convert the resulting set back to a list.
  • Print the resulting list.

Python3




# initializing List
test_list = [4, 24, 8, 10, 12, 23]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing divisor list
div_list = [6, 4]
 
# converting div_list to a set
div_set = set(div_list)
 
# getting a set of all the elements in the test_list that are divisible by all the elements in the div_list set
result_set = {ele for ele in test_list if all(ele % div == 0 for div in div_set)}
 
# converting the resulting set back to a list
res = list(result_set)
 
# printing result
print("All elements multiple of divisor list : " + str(res))


Output

The original list is : [4, 24, 8, 10, 12, 23]
All elements multiple of divisor list : [24, 12]

Time complexity: O(n * m), where n is the length of test_list and m is the length of div_list.
Auxiliary space: O(k), where k is the length of the resulting list (res).



Last Updated : 10 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads