12

I am just coming cross the following python code which confuses me a bit:

 res = self.result[::-1].encode('hex')

The encode stuff is pretty clear, it should be represented as hex value. However, what does this self.result[::-1] mean, especially the colons?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Patrick
  • 989
  • 3
  • 16
  • 23

2 Answers2

18

It represents the 'slice' to take from the result. The first element is the starting position, the second is the end (non-inclusive) and the third is the step. An empty value before/after a colon indicates you are either starting from the beginning (s[:3]) or extending to the end (s[3:]). You can include actual numbers here as well, but leaving them out when possible is more idiomatic.

For instance:

In [1]: s = 'abcdefg'

Return the slice of the string that starts at the beginning and stops at index position 2:

In [2]: s[:3]
Out[2]: 'abc'

Return the slice of the string that starts at the third index position and extends to the end:

In [3]: s[3:]
Out[3]: 'defg'

Return the slice of the string that starts at the end and steps backward one element at a time:

In [4]: s[::-1]
Out[4]: 'gfedcba'

Return the slice of the string that contains every other element:

In [5]: s[::2]
Out[5]: 'aceg'

They can all be used in combination with each other as well. Here, we return the slice that returns every other element starting at index position 6 and going to index position 2 (note that s[:2:-2] would be more idiomatic, but I picked a weird number of letters :) ):

In [6]: s[6:2:-2]
Out[6]: 'ge'

The step element determines the elements to return. In your example, the -1 indicates it will step backwards through the item, one element at a time.

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • Great, that was easy to understand. So essentially this is nothing else than list operations that are used here! – Patrick Nov 13 '12 at 17:32
  • @Patrick You got it :) You'll see it often with strings/lists, and it can even be used for assignment in the case of lists (e.g. `l = ['one', 'two', three']; l[:2] = ['changed']`). Hope it helps! – RocketDonkey Nov 13 '12 at 17:38
  • 1
    if a[::1] means , from 'start' to 'end', step by 1.. then why does a[::-1] mean - from 'end' to 'start' step by -1 ? – d-_-b Sep 05 '19 at 06:44
2

That's a common idiom that reverses a list.

a = ['a', 'b', 'c', 'd']
b = a[::-1]
print b

['d', 'c', 'b', 'a']

You can read about 'extended slices' here.

kreativitea
  • 1,741
  • 12
  • 14
  • 4
    Unfortunate choice of words, it does not reverse a list! `a.reverse()` reverses list `a`. `a[::-1]` returns a reversed copy of `a`. – Junuxx Nov 13 '12 at 17:27
  • 1
    While true, my wording was intentional; that distinction may end up being more confusing to a beginner, they are typically better served by being presented with the simplification and working with the abstraction until it does something unexpected. Variable assignments and mutability are lessons for another day. – kreativitea Nov 13 '12 at 17:34