How does Python represent slices in various cases?
Consider l = [1, 2, 3, 4, 5] print(l is l[:]) t = (1, 2, 3, 4, 5) print(t is t[:]) print(t[1:] is t[1:]) print('aa'[1:] is 'aa'[1:]) print('aaa'[1:] is 'aaa'[1:]) The result is, somewhat surprisingly, False, True, False, True, False. Additionally, if I create objects of types list, tuple and str with large lengths and then create large numbers of slices [1:] of each, only with str is it efficient in terms of time and memory, even though tuples are also immutable and could, just like strings, be represented without copying the range specified by a slice, just by indexing into the contiguous mem
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.