This seems incredibly basic, but this for some reason my list is being modified when calling a function, even though I never change the value of it.
I want to create a new list with values that are doubled, while the original list stays the same. (I need to do it with this method as opposed to something like list comprehensions, since later I want to do different things than double each term).
def DoubleList(listToDouble):
tempList = listToDouble
for i in range(4):
tempList[i] *= 2
return tempList
mainList = [100, 100, 32, 32]
print('List =', mainList) # Should print [100, 100, 32, 32]
doubledList = DoubleList(mainList)
print('Doubled list =', doubledList) # Should print [200, 200, 64, 64]
print('Final List =', mainList) # mainList was never changed, so should print [100, 100, 32, 32]?
Currently it prints:
List = [100, 100, 32, 32]
Doubled list = [200, 200, 64, 64]
Final List = [200, 200, 64, 64]
I would have thought that doubledList
would be doubled (which is working fine), but mainList
would stay the same (currently it also doubles).
Is this doing exactly what it's supposed to and I'm just missing it? Thanks to any that can help! :)