What does the second to last line do here? It doesn't seem to be assigning a variable, printing or anything but I've tried breaking the code in various ways and the line doublelist(somelist)
seems to be necessary but I don't know why.
def doubleList(list):
i=0
while i<len(list):
list[i]=list[i]*2
i=i+1
someList=[34,72,96]
doubleList(someList)
print someList
Functions can modify mutable arguments passed to them. The (poorly-named) list called "list" has (using a non-idiomatic style) each of its elements multiplied by two, in-place. For example:
>>> def inplace(seq):
... seq[0] = 5
...
>>> a = [1,2,3]
>>> print a
[1, 2, 3]
>>> inplace(a)
>>> a
[5, 2, 3]
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