I keep seeing this: s[::-1]
in Python and I don't know what it does. Sorry if this is a question but I'm new to python and generally programming.
Asked
Active
Viewed 375 times
1

Mike Driscoll
- 32,629
- 8
- 45
- 88

Nik
- 11
- 1
-
1it's not pythonic, but it is python. – Rafael Barros Sep 24 '14 at 20:08
-
4How is it non-pythonic? It's the most pythonic way to reverse a sequence that I'm aware of ... – g.d.d.c Sep 24 '14 at 20:09
-
4`reversed` is much clearer. – Peter DeGlopper Sep 24 '14 at 20:10
-
1This question title will not help others when searching this kind of problem. – salmanwahed Sep 24 '14 at 20:15
-
1@PeterDeGlopper `reversed` and `s[::-1]` serve different purposes. You should use the former when you want to *iterate* through a sequence backwards, but use the latter when you actually want the reversed sequence. For strings, using the slice notation is almost always correct. Using `reversed` for strings is actually pretty difficult, you would have to do something like `''.join(reversed(s))`, which is definitely not pythonic. – Roger Fan Sep 24 '14 at 20:39
-
@RogerFan: Or, if you don't know what type something is, but want the same type reversed, `s[::-1]` does what you want; otherwise you need something like `(''.join if issubclass(type(s), str) else type)(reversed(s))`—which still doesn't work for `bytes`/`bytearray`/`unicode`, `pd.DataFrame`, and lots of other seuqences and sequence-like types in the stdlib and popular modules… – abarnert Sep 24 '14 at 21:25
2 Answers
5
It reverses a sequence using slicing.
>>> s = 'hello'
>>> s[::-1]
'olleh'
The slice notation []
is a way to get a subset of some iterable container. It has the syntax
[start : stop : step]
See this post for more details.

Community
- 1
- 1

Cory Kramer
- 114,268
- 16
- 167
- 218
-
2
-
-
1The update's not quite right - not all iterables can be sliced. I think sequence is the most general term: https://docs.python.org/2/glossary.html#term-sequence – Peter DeGlopper Sep 24 '14 at 20:12
-
-
It is admittedly an obscure subject unless you're working with the collections abstract base classes: https://docs.python.org/2/library/collections.html#collections-abstract-base-classes – Peter DeGlopper Sep 24 '14 at 20:15
-
It gets even more confusing language hopping too, because 75% of my day-to-day programming at work is C++, so I have to be careful about Python "list" vs C++ "std::list", etc. Some colloquial terms have technical meanings in other languages and vice versa. – Cory Kramer Sep 24 '14 at 20:16
-
And technically speaking you can overload the slice notation if you want. But... you probably shouldn't do that. – Wayne Werner Sep 24 '14 at 20:17
-
"… some iterable container …" is still wrong. Something can easily be iterable, and a container, but not a sequence. Like, say, `dict`. – abarnert Sep 24 '14 at 21:22
-
1@WayneWerner: Well, you definitely _should_ do it if you're trying to build a custom sequence, or a not-quite-sequence like a numpy array or pandas frame… – abarnert Sep 24 '14 at 21:23
0
Slicing is an operation that gives you some elements out of a sequence.
s[a:b:c]
means "the items starting at a
, stopping at b
, with a step of c
".
If you have s[::-1]
that means "the whole sequence, going backwards".

khelwood
- 55,782
- 14
- 81
- 108