Aug 2, 2024 07:57 AM
(This post was last modified: Aug 2, 2024 08:01 AM by Azythum.)
Hello hackforums coders,
I'm trying to solve a challenge on HackerRank. The challenge is more complex. But here is a small piece of code. I have the feeling that understanding this could allow me to understand more complex things.
Here is the result in the console:
[[1]]
[[2], [2]]
[[1]]
[[1], [2]]
Normally, the two small pieces of code should produce the same outputs. But in one case, it gives me [[2], [2]], and in the other, it gives me [[1], [2]]. For me, [[1], [2]] is correct.
I'm trying to solve a challenge on HackerRank. The challenge is more complex. But here is a small piece of code. I have the feeling that understanding this could allow me to understand more complex things.
Code
a = []
c = [0]
c[0]=1
a.append(c)
print(a)
c[0]=2
a.append(c)
print(a)
b=[]
b.append([1])
print(b)
b.append([2])
print(b)Here is the result in the console:
[[1]]
[[2], [2]]
[[1]]
[[1], [2]]
Normally, the two small pieces of code should produce the same outputs. But in one case, it gives me [[2], [2]], and in the other, it gives me [[1], [2]]. For me, [[1], [2]] is correct.


