Answer the question
In order to leave comments, you need to log in
How to multiply every NINTH element of a list by 2?
Hello! A question. How to multiply every ninth element of a list by 2?
Tried like this, it doesn't work. I don't understand, what is the correct syntax?
The array samples_null_list
contains floating point numbers.
step_null_list = []
for i in range(samples_null_list[0], samples_null_list[-1], samples_null_list[8]):
samples_list[i] *= 2
step_null_list.append(samples_list[i])
Answer the question
In order to leave comments, you need to log in
Your code probably solves some problem, but not exactly described by you.
See range(start, stop[, step]) :
new_list = []
for i, x in enumerate(my_list):
if (i+1) % 9 == 0:
x *= 2
new_list.append(x)
Use index access. And it will be for i in tange(list_length)...
>>> lst = [1, 2, 3, 4, 5] * 10
>>>
>>> out = [n * 2 if i % 9 == 0 else n
... for i, n in enumerate(lst, 1)]
>>> out
[1, 2, 3, 4, 5, 1, 2, 3, 8, 5, 1, 2, 3, 4, 5, 1, 2, 6, 4, 5, 1, 2, 3, 4, 5, 1, 4, 3, 4, 5, 1, 2, 3, 4, 5, 2, 2, 3, 4, 5, 1, 2, 3, 4, 10, 1, 2, 3, 4, 5]
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question