-2

How this code works? Although the column is not defined in nested list then how thee value gets changed? How 4th line of code works even if we have everything in a row in nested list?

a=["a", "1"]
b=["b", "2"]
c=[a,b]
c[0][1]="m"
print(f"{a}\n{b}")
n_xlade
  • 1
  • 1
  • You might want to read [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html) – Matthias Oct 08 '22 at 20:41

1 Answers1

2

Here is an understanding how the python code works,

In [1]: a=["a", "1"]
     ...: b=["b", "2"]
     ...: c=[a,b]

In [2]: c
Out[2]: [['a', '1'], ['b', '2']]

In [3]: id(c[0])
Out[3]: 4609196992

In [4]: id(a)
Out[4]: 4609196992

In [5]: id(a) == id(c[0])
Out[5]: True

c is a list of list with a and b. So when you change c[0] it's change a as well.

Read about id()

Rahul K P
  • 15,740
  • 4
  • 35
  • 52