0

I have an array which contains some elements.

How by doing arr[::-1] sort the entire array?

What is the logic behind that?

2
  • 3
    arr[::-1] does not sort a list, it only reverses a list. Commented Aug 16, 2016 at 12:03
  • Sorting the array would change the order of all its elements such that arr[i] <= arr[i+1]. arr[::-1] doesn't do that, it just reverses the order in which they occur in the array. Commented Aug 16, 2016 at 13:09

1 Answer 1

5

This is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string. Example:

>>> 'hello world'[::-1]
'dlrow olleh'

See also

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.