'Finding the minimum multiple of a specific number in a python list

Imagine that you have a list of numbers and you would like to find the index of numbers meeting a certain condition (say > 3). We would then like to have the smallest index (meeting the above condition) being a multiple of a certain number (here 4). Below might be a good start. What would you suggest for the rest of the code?

a = [1, 2, 3, 1, 2, 3, 5, 8, 9, 10 ,12, 12, 15, 16]
b = 4

output = [i for i, x in enumerate(a) if x > 3]

print(output) '''



Solution 1:[1]

If you want to find the minumim index, you can use the min() function:

a = [1, 2, 3, 1, 2, 3, 5, 8, 9, 10 ,12, 12, 15, 16] 
b = 4

output = [i for i, x in enumerate(a) if x > 3]

print(output, min(output))

Solution 2:[2]

The indices are already obtained by the code given above. All you need to do is find the smallest index divisible by b. You can achieve it as follows:

a = [1, 2, 3, 1, 2, 3, 5, 8, 9, 10 ,12, 12, 15, 16]
b = 4
# outputGT3 = output of all numbers > 3
outputGT3 = [i for i, x in enumerate(a) if x > 3]
print(outputGT3)
# outputDBb = output of all numbers divisible by b
outputDBb = [x for i, x in enumerate(outputGT3) if x % b==0]
print(outputDBb[0])
# since outputDBb is already sorted, the 1st element is the smallest.

Solution 3:[3]

This question is unrelated to lists:

a, b = 3, 4
n = a + b/2
result = n - (n%b)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cardstdani
Solution 2 Anshumaan Mishra
Solution 3 Jeremy