this post was submitted on 13 Jul 2024
62 points (98.4% liked)

technology

23113 readers
172 users here now

On the road to fully automated luxury gay space communism.

Spreading Linux propaganda since 2020

Rules:

founded 4 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 5 points 1 month ago (3 children)

Of course, in Python, you can slice with [start:end:step], so it is valid to do list[:3:3] for double the happiness. Not that you'd ever practically do so.

[–] [email protected] 2 points 1 month ago* (last edited 1 month ago) (1 children)

It's functionally identical to list[0] so you could definitely just refactor your code to use list[:3:3]

[–] [email protected] 2 points 1 month ago (1 children)

I don't think that's equivalent. I think the former would return e0 and the latter would return [e0]

[–] [email protected] 1 points 1 month ago* (last edited 1 month ago)

You're right, it would return a list containing the first element of the sliced list.

So:

lst[:3:3] == [lst[0]]

Well, technically a sequence containing the first element of whatever sequence you sliced. Take advantage of implementing magic methods and you too can abuse slice notation with your classes.

load more comments (1 replies)