Let's say I have a list, mylist
, and I define it to be [1, 2, 3, 4, 5]
. How is there a simple way to double every element of the list from within a for
loop?
I know you could do something like:
for i in range(len(mylist)):
mylist[i] *= 2
I also know that you could make a list comprehension:
mylist = [2*i for i in range(1, 6)
But is there a way to do it like this?
for num in mylist:
#code goes here
I tried doing something like:
for num in mylist:
num *= 2
but num
is a local variable, so this doesn't modify mylist
, just num
. Does anyone know a good way to do this?