0

I found the earlier responses to the above question after encountering the problem with coding using code such as: [[0] * 6] * 3 to create a matrix.

First, I thank all the responders for pointing out correct methods and also how to use the id command to see the results, but I still don't understand why python say two lists are the same and print the same apparent results when one is a list of lists and one a list of identical rows? Does == just mean the two contain the same elements and order, but not necessarily the same structure? If so, is there a better comparison command for comparing lists which takes into account their structure? I am obviously missing something as the lists look identical as to the punctuation (spacing, commas,bracketing,values, types, response to equivalency, etc.) yet are definitely not.

I really want to understand why == says two lists are the same, yet they obviously are not.

I did a lot of programming in Pascal and SAS macro language never remember anything remotely similar happening.

Just pointing me to where to find a good discussion of this will be most appreciated.

JBRIII
  • 1
  • 1
  • Does this answer your question? [Python initializing a list of lists](https://stackoverflow.com/questions/12791501/python-initializing-a-list-of-lists) – j1-lee Oct 17 '21 at 04:52
  • 1
    You are asking a hard question that people have written Phd theses about. There are many different definitions of two things being "equal". In Python, two numbers are the == if they have the same value, even if they have different types. Thus 2 == 2.0 even though one is an integer and the other is a float. – Frank Yellin Oct 17 '21 at 04:53
  • Thanks, at least your answer does not make it seem like a stupid question. – JBRIII Oct 18 '21 at 22:32
  • Thanks, got three great answers, although it still seems like a bad way to do programming to me. But not of us wrote python. – JBRIII Oct 18 '21 at 22:34
  • Yeah, it's pretty weird rule at first, but it makes sense. Either way, you cannot really change it. – krmogi Oct 18 '21 at 22:38

1 Answers1

1

The == operator compares the value or equality of two objects. Python will say that 2 objects are == even if they are different types, because they hold the same information/values.

a = [[0] * 6] * 3
rows, cols = 3,6
b = [([0]*cols) for i in range(rows)]

c = a
print(a)
print(b)
print(a == b)
print(a is b)
print(a is c)

2 objects created different ways or have different formatting will be == because their contents are the same. However, a is not b. This is because the Python is operator checks whether two variables point to the same object in memory. a is c, because both go to the same object in memory, a.

Here's a pretty great article going in depth about this: https://towardsdatascience.com/comparison-in-python-is-not-as-simple-as-you-may-think-a83eec69ab54

krmogi
  • 2,588
  • 1
  • 10
  • 26
  • Thank, while you make it absolutely clear, it seems like a flaw to me. Like saying you == me is true, both humans, but you is me, False as we occupy different points in space/time. – JBRIII Oct 18 '21 at 22:29
  • @JBRIII How so? Could you please elaborate what you mean by flaw, and how I could explain it to you? – krmogi Oct 18 '21 at 22:39