Most Developers Failed with this Senior-Level Python Interview Question
What is the difference between [0] * 3 and [0, 0, 0]?
Okay, here is another interview question for senior Python developers.
We have three different ways to generate a list in 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
?
Imagine you were asked this question during a tech interview, what’s your answer?
You may think they are the same list since all of them are equal to [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:
sys.getsizeof(object[, default])
Return the size of an object in bytes. The object can be any type of…