0

Here the goes,

list_of_list = [[]] * 10
list_of_list[0].append(1)
print(list_of_list)

There is a variable list_of_list which is a 10 element list containing empty list []. So initially the list is [[],[],[],[],[],[],[],[],[],[]]. So in the next line I have appended 1 to the first element of the variable list_of_list. So my expected result should be, [[1],[],[],[],[],[],[],[],[],[]]. But what I get is, [[1],[1],[1],[1],[1],[1],[1],[1],[1],[1]].

So why is this happening? How can I get my achieved result?

  • It's a ten element list **containing the same list 10 times**. That is how `some_list * n` works where `n` is an `int` – juanpa.arrivillaga Jun 22 '20 at 22:51
  • you need `list_of_list = [[] for _ in range(10)]` This question has been asked several times on stack, see the previous answers linked above. – Chris Doyle Jun 22 '20 at 22:54
  • Whenever I search in StackOverflow, I don't find answer. But when I ask my question I find it already asked. (In the duplication section, I find the links). So, StackOverflow bot is smarter than me to find questions? – Md Ashraful Islam Jun 22 '20 at 22:56
  • 1
    @MdAshrafulIslam it isn't bots. It is volunteer moderators. People who spend too much time on stack overflow they might be confused for bots :) – juanpa.arrivillaga Jun 22 '20 at 23:03

0 Answers0