Computer Science

Does the slice operator always produce a new tuple ?

Python Tuples

5 Likes

Answer

No, the slice operator does not always produce a new tuple. If the slice operator is applied on a tuple and the result is the same tuple, then it will not produce a new tuple, it will return the same tuple as shown in the example below:

a = (1, 2, 3)  
print(a[:])

Slicing tuple a using a[:] results in the same tuple. Hence, in this case, slice operator will not create a new tuple. Instead, it will return the original tuple a.

Answered By

4 Likes


Related Questions