[issue38245] Why am I getting inconsistent results in this simple List assignment?

2019-09-21 Thread Srinivasan Samuel

Srinivasan Samuel  added the comment:

Thanks Ammar for your time, service and reply. It was really helpful. I learned 
some thing more.God Bless you.Srinivasan Samuel
On Saturday, September 21, 2019, 10:50:32 PM GMT+5:30, Ammar Askar 
 wrote:  

Ammar Askar  added the comment:

Check out this part of the FAQ: 
https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list

Essentially, when you did `C = 2*[[]]`, what happens is that the SAME empty 
list is placed into C[0] and C[1]. Whereas when you do `M = [[],[]]`, you're 
creating two different lists. You can confirm this using:

>>> C = 2*[[]]
>>> C[0] is C[1]
True
>>> M = [[],[]]
>>> M[0] is M[1]
False

--
nosy: +ammar2
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38245] Why am I getting inconsistent results in this simple List assignment?

2019-09-21 Thread Ammar Askar


Ammar Askar  added the comment:

Check out this part of the FAQ: 
https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list

Essentially, when you did `C = 2*[[]]`, what happens is that the SAME empty 
list is placed into C[0] and C[1]. Whereas when you do `M = [[],[]]`, you're 
creating two different lists. You can confirm this using:

>>> C = 2*[[]]
>>> C[0] is C[1]
True
>>> M = [[],[]]
>>> M[0] is M[1]
False

--
nosy: +ammar2
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38245] Why am I getting inconsistent results in this simple List assignment?

2019-09-21 Thread Srinivasan Samuel


New submission from Srinivasan Samuel :

Here is the my direct cut & paste from my Python 3.7.3 Shell
>>> C = 2*[[]]
>>> C
[[], []]
>>> 
>>> M = [[],[]]
>>> M
[[], []]
>>> 
>>> C == M
True
>>> 
>>> M[0].append(5)
>>> M
[[5], []]
>>> 
>>> C[0].append(5)
>>> C
[[5], [5]]
>>> 
>>> C == M
False
>>>

--
messages: 352945
nosy: pysolo
priority: normal
severity: normal
status: open
title: Why am I getting inconsistent results in this simple List assignment?
type: behavior
versions: Python 3.7

___
Python tracker 
<https://bugs.python.org/issue38245>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: dictionary error: list assignment index out of range

2011-01-28 Thread Octavian Rasnita
From: Vaduvoiu Tiberiu 
   Well, to quote firefox: this is embarrassing. I've realized the dictionary 
initialization is wrong, as [] means its a tuple, I should use {}. That's why I 
 don't like working nights..it's only in the morning when you start seeing 
things better. I apologize for the mail. Cheers


  [] is for lists.
  () is for tuples.

  HTH.

  Octavian
-- 
http://mail.python.org/mailman/listinfo/python-list


dictionary error: list assignment index out of range

2011-01-27 Thread Vaduvoiu Tiberiu
Hy everyone, I'm trying to learng python for a week or two and there's a thing 
that is really disturbing me as I do not understand what the problem is. I'm 
trying to use a dictionary to remember when a user has visited a city. Code is 
really basic:

in the class init method I added
self.visited = []

and in the function where i check if city was visited:
cityNumber = 1 #example
if (not cityNumber in self.visited):
#do some stuff
self.visited[cityNumber] = true

Apparently the last line causes the error: list assignment index out of range. 
I 
read that this is the simplest way to assign a value to a 
dictionary(dict[key]=value). So why is the error appearing?? Thanks a lot in 
advance



  -- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary error: list assignment index out of range

2011-01-27 Thread Vaduvoiu Tiberiu
Well, to quote firefox: this is embarrassing. I've realized the dictionary 
initialization is wrong, as [] means its a tuple, I should use {}. That's why I 
don't like working nights..it's only in the morning when you start seeing 
things 
better. I apologize for the mail. Cheers






From: Vaduvoiu Tiberiu vaduvoiut...@yahoo.com
To: python-list@python.org
Sent: Fri, January 28, 2011 9:34:57 AM
Subject: dictionary error:  list assignment index out of range


Hy everyone, I'm trying to learng python for a week or two and there's a thing 
that is really disturbing me as I do not understand what the problem is. I'm 
trying to use a dictionary to remember when a user has visited a city. Code is 
really basic:

in the class init method I added
self.visited = []

and in the function where i check if city was visited:
cityNumber = 1 #example
if (not cityNumber in self.visited):
#do some stuff
self.visited[cityNumber] = true

Apparently the last line causes the error: list assignment index out of range. 
I 
read that this is the simplest way to assign a value to a 
dictionary(dict[key]=value). So why is the error appearing?? Thanks a lot in 
advance


  -- 
http://mail.python.org/mailman/listinfo/python-list


list assignment using concatenation *

2006-02-24 Thread liquid
If I do:

a = [ [0,0,0], [0,0,0], [0,0,0] ]
a[0][1] = 1

I get:

a = [ [0,1,0],[0,0,0],[0,0,0] ]

as expected

But if I do:

a = [ [0] * 3 ] * 3
a[0][1] = 1

I get

a = [[0,1,0],[0,1,0],[0,1,0]]

AFAIC, * is supposed to generate multiple copies of the given token.
Therefore I thought both cases would be the same, but they are not.

Can anyone explain to me what exactly is going on in the second case?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list assignment using concatenation *

2006-02-24 Thread Steve R. Hastings
 if I do:
 
 a = [ [0] * 3 ] * 3
 a[0][1] = 1
 
 I get
 
 a = [[0,1,0],[0,1,0],[0,1,0]]

The language reference calls '*' the repetition operator.  It's not
making copies of what it repeats, it is repeating it.

Consider the following code:

 a = []
 b = []
 a == b
True
 a is b
False

 a = b = []
 a is b
True
 a.append(1)
 a
[1]
 b
[1]


Each time you use [], you are creating a new list.  So the first code sets
a and b to two different new lists.

The second one, a = b = [], only creates a single list, and binds both a
and b to that same list.

In your example, first you create a list containing [0, 0, 0]; then you
repeat the same list three times.

 a = [[0]*3]*3
 a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
 a[0] is a[1]
True
 a[0] is a[2]
True

When you run [0]*3 you are repeating 0 three times.  But 0 is not mutable.
When you modify a[0] to some new value, you are replacing a reference to
the immutable 0 with some new reference.  Thus, [0]*3 is a safe way to
create a list of three 0 values.

When you have a list that contains three references to the same mutable,
and you change the mutable, you get the results you discovered.
-- 
Steve R. HastingsVita est
[EMAIL PROTECTED]http://www.blarg.net/~steveha

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list assignment using concatenation *

2006-02-24 Thread Steve R. Hastings
I suggest you should build your list using a list comprehension:

a = [[0]*3 for i in range(3)]
a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
a[0][1] = 1
[[0, 1, 0], [0, 0, 0], [0, 0, 0]]

-- 
Steve R. HastingsVita est
[EMAIL PROTECTED]http://www.blarg.net/~steveha

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list assignment

2006-02-23 Thread Bernhard Herzog
Norvell Spearman [EMAIL PROTECTED] writes:

 Lutz and Ascher have tuple and list assignment as separate entries in
 their assignment statement forms table so I was expecting there to be
 some difference; thanks for setting me straight.

In older Python versions there was a difference between list unpacking
and tuple unpacking.  The former would only work with lists and the
latter with tuples.  With Python 1.5, both were unified into a more
general sequence unpacking, but for backwards compatibility both
syntaxes were kept.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


list assignment

2006-02-22 Thread Norvell Spearman
In Learning Python, by Lutz and Ascher, there's a table showing different 
assignment statement forms.  One form shown is list assignment.  The authors 
give this as an example:

 [spam, ham] = ['yum', 'YUM']

I don't see how this is any different than a tuple unpacking assignment:

  a, b = 1, 2
  a, b
 (1, 2)
  [a, b] = [1, 2]
  a, b
 (1, 2)

In both instances the names a and b are both mapped to 1 and 2 so why are there 
two different forms?

Thanks for any answers.

--
Norvell Spearman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list assignment

2006-02-22 Thread Raymond Hettinger
  [spam, ham] = ['yum', 'YUM']

 I don't see how this is any different than a tuple unpacking assignment:

   a, b = 1, 2

It's not different.  They are ways of writing the same thing.


Raymond Hettinger

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list assignment

2006-02-22 Thread Jeffrey Schwab
Raymond Hettinger wrote:
 [spam, ham] = ['yum', 'YUM']

I don't see how this is any different than a tuple unpacking assignment:

  a, b = 1, 2
 
 
 It's not different.  They are ways of writing the same thing.

TMTOWTDI, after all. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list assignment

2006-02-22 Thread Norvell Spearman
Raymond Hettinger wrote:
 It's not different.  They are ways of writing the same thing.

Lutz and Ascher have tuple and list assignment as separate entries in their 
assignment statement forms table so I was expecting there to be some 
difference; thanks for setting me straight.

--
Norvell Spearman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list assignment

2006-02-22 Thread Norvell Spearman
Jeffrey Schwab wrote:
 TMTOWTDI, after all. :)

A bit ironic that that's the official motto of Perl, don't you think?

--
Norvell Spearman
-- 
http://mail.python.org/mailman/listinfo/python-list