Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the def line do in python [closed]

Tags:

python

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
like image 206
ZCJ Avatar asked Feb 22 '23 12:02

ZCJ


1 Answers

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]
like image 171
DSM Avatar answered Mar 04 '23 14:03

DSM