Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27122

Re: A difficulty with lists

From Madison May <worldpeaceagentforchange@gmail.com>
Newsgroups comp.lang.python
Subject Re: A difficulty with lists
Date 2012-08-15 16:56 -0700
Organization http://groups.google.com
Message-ID <275392ae-09e2-469d-a9e3-43a9f50eece5@googlegroups.com> (permalink)
References <jvp75j$opr$1@news.albasani.net>

Show all headers | View raw


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)
> 
> 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

I've modified your code slightly so you can see what's happening with u in the middle of function xx.  Take a look:

u=[1,2,3,4]

def xx(nlist):
    print("xx(u)\n")
    print("At first, u and nlist refer to the same list")
    print("nlist: %s   u: %s\n" % (nlist, u))
 
    nlist+=[999]

    print("nlist+=[999]\n")
    print("The list has been modified in place.  u and nlist are still equal")
    print("nlist: %s   u: %s\n" %(nlist, u))
 
    nlist=nlist[:-1]
 
    print("nlist=nlist[:1]\n")
    print("Now nlist refers to a new list object in memory that was created by")
    print("taking a slice of u.  u and nlist are no longer equal.")
    print("nlist: %s   u: %s" %(nlist, u))
    
xx(u)

Here's the output:


xx(u)

At first, u and nlist refer to the same list
nlist: [1, 2, 3, 4]   u: [1, 2, 3, 4]

nlist+=[999]

The list has been modified in place.  u and nlist are still equal
nlist: [1, 2, 3, 4, 999]   u: [1, 2, 3, 4, 999]

nlist=nlist[:1]

Now nlist refers to a new list object in memory that was created by
taking a slice of u.  u and nlist are no longer equal.
nlist: [1, 2, 3, 4]   u: [1, 2, 3, 4, 999]


Thank you, Rob Day, for explaining a some of what's happening behind the scenes.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

A difficulty with lists Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-08-06 21:50 +0200
  Re: A difficulty with lists MRAB <python@mrabarnett.plus.com> - 2012-08-06 21:19 +0100
  Re: A difficulty with lists Madison May <worldpeaceagentforchange@gmail.com> - 2012-08-15 14:12 -0700
    Re: A difficulty with lists Terry Reedy <tjreedy@udel.edu> - 2012-08-15 20:21 -0400
      Re: A difficulty with lists Madison May <worldpeaceagentforchange@gmail.com> - 2012-08-16 06:46 -0700
  Re: A difficulty with lists Madison May <worldpeaceagentforchange@gmail.com> - 2012-08-15 16:56 -0700
  Re: A difficulty with lists Cheng <chbeh88@googlemail.com> - 2012-08-20 13:43 -0700

csiph-web