references ???

Skip Montanaro skip at pobox.com
Thu Nov 1 11:48:52 EST 2001


    Thomas> l=[1,2,3,4,5,6]
    Thomas> for i in l:i=0
    Thomas> print l

    Thomas> l is still [1,2,3,4,5,6]. How to make changes made to i also
    Thomas> apply to l ?

Everything in Python is a reference, so rebinding the name "i" won't have
any effect on the list that is bound to the name "l".  You need to change
the elements of l:

    for i in range(len(l)):
        l[i] = 0
    print l

In the end you should have a list full of zeroes.

P.S.  For purposes of clarity, names like "l" and "i" are probably not the
best choice. ;-)

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list