0

I wanted to know how do append function work for sublists of lists in Python

If we create a list as:

a =  [[]]*4 

and append as:

a[0].append(10)

The output of printing a was:

[[10],[10],[10,[10]]

Why does 10 get copied in all four sublists while it should just for the first one??? i.e.

[[10],[],[],[]]
ScottC
  • 3,941
  • 1
  • 6
  • 20
Charles
  • 13
  • 3
  • 2
    In other words, you absolutely do not have four sublists, you have *one* sublist with four references to it. – jasonharper Dec 20 '22 at 03:28
  • 1
    I'd like to especially highlight [this article](https://nedbatchelder.com/text/names1.html) in the linked answer, which is a must-read for newcomers to python. – Aaron Dec 20 '22 at 03:35

0 Answers0