What is the difference between [0] * 3 and [0, 0, 0]?
Here’s another interview question for senior Python developers:
We have three different methods to create a list in Python:
python
list1 = [0] * 3
list2 = [0, 0, 0]
list3 = [0 for i in range(3)]
So, what is the difference between `list1`, `list2`, and `list3`?
If you were asked this question during a technical interview, what would your answer be?
At first glance, you might think all three lists are identical since they all equal `[0, 0, 0]`.
It’s right from the elements’ perspective, all of them are a list that contains three 0
.
But, if you try to check their byte size, you will find a strange result: