Python program to print elements which are multiples of elements given in a list
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
test_list = [ 4 , 24 , 8 , 10 , 12 , 23 ]
print ( "The original list is : " + str (test_list))
div_list = [ 6 , 4 ]
res = [ele for ele in test_list if all (ele % j = = 0 for j in div_list)]
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
test_list = [ 4 , 24 , 8 , 10 , 12 , 23 ]
print ( "The original list is : " + str (test_list))
div_list = [ 6 , 4 ]
res = list ( filter ( lambda ele: all (ele % j = = 0 for j in div_list), test_list))
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
test_list = [ 4 , 24 , 8 , 10 , 12 , 23 ]
print ( "The original list is : " + str (test_list))
div_list = [ 6 , 4 ]
res = []
for ele in test_list:
flag = True
for div in div_list:
if ele % div ! = 0 :
flag = False
break
if flag:
res.append(ele)
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
test_list = [ 4 , 24 , 8 , 10 , 12 , 23 ]
print ( "The original list is : " + str (test_list))
div_list = [ 6 , 4 ]
div_set = set (div_list)
result_set = {ele for ele in test_list if all (ele % div = = 0 for div in div_set)}
res = list (result_set)
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
Share your thoughts in the comments
Please Login to comment...