I have been programming for a while in Java and C++. However, I am trying to learn Python and I am trying to initialize a two-dimensional array in Python. Why does it take only the last two inputs?
I tried everything, but I can't understand why it is not working how it is supposed to be.
def prog2():
x = int(input('x> '))
y = int(input('y> '))
pole = [[0] * x] * y
for a in range(x):
for b in range(y):
pole[a][b] = int(input('> '))
print(pole)
prog2()
When I define x
and y
as 2, and the following four inputs as 1, 2, 3, 4,
I expect the output to be [[1, 2], [3, 4]].
However I am getting [[3, 4],[3, 4]]
x> 2
y> 2
> 1
> 2
> 3
> 4
[[3, 4], [3, 4]]
Thank you for your patience and help. :) I am a novice in Python.