DEV Community

Discussion on: 3 cool things you can do with Python indexing

Collapse
 
habilain profile image
David Griffin

Two important things: Firstly, the second two examples are slicing, not indexing.

Second off, slicing doesn't quite work how you think it does (assuming you think x[::-1] == reversed(x), which your post seems to imply). Creating a slice creates a copy of that list segment. Pythons memory model means you don't copy the contents itself, but you're creating more references to the objects in the copy at around 28 bytes an element. So if you're memory constrained, or working with really big lists, you should probably use generators - like reversed (because reversed(x) != x[::-1]; they're not even the same type).

Collapse
 
mariamodan profile image
Maria

Thank you for your observations! I didn't mean to imply that think x[::-1] == reversed(x), just that reversed is another way to reverse a list. It completely escaped my mind that slicing creates new lists, I've edited the post to include it :)