Supermarket Queue, copy()

Hello,

I have a question regarding answer code in Supermarket Queue practice problem.

link: https://app.dataquest.io/m/1000352/object-oriented-python-practice-problems/6/supermarket-queue

Snippet of code:
def str(self):
tmp = self.elements[self.front_index:].copy()
tmp.reverse()
return str(tmp)

Could you please explain why the copy() method has been used in this particular example? Without using that method the output is still correct.

Maybe this is not relevant, if so please let me know.

Thank you in advance.

2 Likes

I don’t think that is required in this case because we are here doing slicing and slicing with list return shallow copy so. - reference

But you should also know about when to use copy. Here are few good answer - https://stackoverflow.com/questions/2612802

2 Likes