Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26655
| Date | 2012-08-06 21:19 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: A difficulty with lists |
| References | <jvp75j$opr$1@news.albasani.net> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3029.1344284341.4697.python-list@python.org> (permalink) |
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: [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?
>
This:
nlist+=[999]
appends to the list, making it [1, 2, 3, 4, 999].
This:
nlist=nlist[:-1]
gets a slice of the list and then binds it to the local name 'nlist'.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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