For an array of numbers, I must use list comprehension to find elements that:
Are divisible by 6
Their position is also divisible by 6
For example if the input is:
6 12 8 9 1 18
The output should be:
18
Here's what I have already done.
print(list(map(int, input().split()))[5::6])
I don't know how to find numbers that are divisible by 6
.
That's how you can do it:
[el for idx, el in enumerate(lst) if idx % 6 == 0 and el % 6 == 0]
Note that typically indexes start from 0
and so the correct answer is 6
, not 18
. If you want to index from 1
then tweak the above:
[el for idx, el in enumerate(lst, 1) if idx % 6 == 0 and el % 6 == 0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With