A difficulty with lists

Mok-Kong Shen mok-kong.shen at t-online.de
Mon Aug 6 15:50:13 EDT 2012


I ran the following code:

def xx(nlist):
   print("begin: ",nlist)
   nlist+=[999]
   print("middle:",nlist)
   nlist=nlist[:-1]
   print("final: ",nlist)

u=[1,2,3,4]
print(u)
xx(u)
print(u)

and obtained the following result:

[1, 2, 3, 4]
begin:  [1, 2, 3, 4]
middle: [1, 2, 3, 4, 999]
final:  [1, 2, 3, 4]
[1, 2, 3, 4, 999]

As beginner I couldn't understand why the last line wasn't [1, 2, 3, 4].
Could someone kindly help?

M. K. Shen



More information about the Python-list mailing list