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


Groups > comp.lang.python > #105491

Re: Convert list to another form but providing same information

Newsgroups comp.lang.python
Date 2016-03-22 07:23 -0700
References <1010f2cb-21f9-495b-8af4-03ad209b4c1e@googlegroups.com> <eb002b45-6b6b-422c-8a9b-ee36c95109ca@googlegroups.com>
Message-ID <1ec91e1c-9d0e-448b-a7b6-194ac105f8e2@googlegroups.com> (permalink)
Subject Re: Convert list to another form but providing same information
From anantguptadbl@gmail.com

Show all headers | View raw


On Tuesday, March 22, 2016 at 12:01:10 AM UTC+5:30, Maurice wrote:
> Just figured why:
> 
> If I type this on the kernel:
> 
> weirdList = [[0]*3]*5
> 
> weirdList
> Out[257]: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
> 
> weirdList[0][0] = 1
> 
> weirdList
> Out[259]: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]
> 
> All first elements of the sublists also changes. I dunno why...

When we write [[0] * 3] * 5, the [0] * 3 list is repeated 5 times
its something like
list,list,list,list,list

if we change the 1st element of list, it gets reflected on all the 5 copies

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


Thread

Convert list to another form but providing same information Maurice <mauricioliveiraguarda@gmail.com> - 2016-03-21 11:26 -0700
  Re: Convert list to another form but providing same information Maurice <mauricioliveiraguarda@gmail.com> - 2016-03-21 11:30 -0700
    Re: Convert list to another form but providing same information Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-21 19:24 +0000
    Re: Convert list to another form but providing same information anantguptadbl@gmail.com - 2016-03-22 07:23 -0700
  Re: Convert list to another form but providing same information Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-21 19:21 +0000
  Re: Convert list to another form but providing same information Peter Otten <__peter__@web.de> - 2016-03-21 20:22 +0100
  Re: Convert list to another form but providing same information Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-21 20:03 +0000
    Re: Convert list to another form but providing same information Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 14:12 -0600
      Re: Convert list to another form but providing same information Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-22 00:31 +0000
        Re: Convert list to another form but providing same information Chris Angelico <rosuav@gmail.com> - 2016-03-22 11:58 +1100
        Re: Convert list to another form but providing same information Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-22 15:34 +1100
    Re: Convert list to another form but providing same information Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 14:14 -0600
  Re: Convert list to another form but providing same information Steven D'Aprano <steve@pearwood.info> - 2016-03-22 11:13 +1100
  Re: Convert list to another form but providing same information Paul Rubin <no.email@nospam.invalid> - 2016-03-21 18:35 -0700
    Re: Convert list to another form but providing same information Steven D'Aprano <steve@pearwood.info> - 2016-03-22 21:49 +1100

csiph-web