Re: A difficulty with lists

2012-08-20 Thread Cheng
On Monday, August 6, 2012 12:50:13 PM UTC-7, Mok-Kong Shen wrote: > 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) >

Re: A difficulty with lists

2012-08-16 Thread Madison May
On Wednesday, August 15, 2012 8:21:22 PM UTC-4, Terry Reedy wrote: > On 8/15/2012 5:58 PM, Rob Day wrote: > > Yeah, my apologies for any confusion I created. Although I suppose my > explanation would be somewhat true for immutable objects since they can't be > modified in-place (any modificatio

Re: A difficulty with lists

2012-08-15 Thread Terry Reedy
On 8/15/2012 5:58 PM, Rob Day wrote: > Madison May wrote: The list nlist inside of function xx is not the same as the variable u outside of the function: nlist and u refer to two separate list objects. When you modify nlist, you are not modifying u.

Re: A difficulty with lists

2012-08-15 Thread Madison May
On Monday, August 6, 2012 3:50:13 PM UTC-4, Mok-Kong Shen wrote: > 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) > >

Re: A difficulty with lists

2012-08-15 Thread Rob Day
> The list nlist inside of function xx is not the same as the variable u > outside of the function: nlist and u refer to two separate list objects. > When you modify nlist, you are not modifying u. > Well - that's not quite true. Before call

Re: A difficulty with lists

2012-08-15 Thread Madison May
On Monday, August 6, 2012 3:50:13 PM UTC-4, Mok-Kong Shen wrote: > 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) > >

Re: A difficulty with lists

2012-08-06 Thread MRAB
On 06/08/2012 20:50, Mok-Kong Shen wrote: 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:

Re: A difficulty with lists

2012-08-06 Thread Chris Kaynor
On Mon, Aug 6, 2012 at 12:50 PM, Mok-Kong Shen wrote: > I ran the following code: > > def xx(nlist): > print("begin: ",nlist) > nlist+=[999] > This is modifying the list in-place - the actual object is being changed to append 999. This can happen because lists are mutable data types in Python

A difficulty with lists

2012-08-06 Thread Mok-Kong Shen
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