0

when I try to assign a list to 2 variables, the following 2 ways get different results?

# way1
a,b =  [[None,None],[None,None]]
# way2
c,d = [[None,None]]*2

a and b have different addresses on memory caches, while c and d have the same one.

print(id(a),id(b))
# out: 5243249856 5243249472

print(id(c),id(d))
# out: 5243246016 5243246016

The right-side lists are completely the same, the only difference is the way to create the list (way2 uses a '*' to expand the list). However, the left-side get different results. How is this happen? any one have any idea?

yize
  • 1
  • also: https://stackoverflow.com/questions/974931/multiply-operator-applied-to-listdata-structure – Nir Alfasi May 19 '21 at 06:28
  • You seem to have answered your own question, `[[None,None],[None,None]]` creates a list with two references to two different lists, `[[None,None]]*2` creates a list with two references to the same list – juanpa.arrivillaga May 19 '21 at 06:28

0 Answers0