I have a list, let\'s say: list = [6,2,6,2,6,2,6]
, and I want it to create a new list with every other element multiplied by 2 and every other element multiplie
You can use slice assignment and list comprehension:
l = oldlist[:]
l[::2] = [x*2 for x in l[::2]]
Your solution is wrong because:
res
is declared as a number and not a listmulti
Here's your code, corrected:
def multi(lst):
res = list(lst) # Copy the list
# Iterate through the indexes instead of the elements
for i in range(len(res)):
if i % 2 == 0:
res[i] = res[i]*2
return res
print(multi([12,2,12,2,12,2,12]))