60

Working on a python assignment and was curious as to what [:-1] means in the context of the following code: instructions = f.readline()[:-1]

Have searched on here on S.O. and on Google but to no avail. Would love an explanation!

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
Matt.
  • 1,306
  • 2
  • 13
  • 20
  • Sometimes -1 is used to express the end of an array of things. My guess is this means to read from beginning to the end of the line (but just a guess, hence not an official answer). – Daedalus Mar 20 '13 at 21:37
  • I'm not sure it's a dup, because of that "in the context of…" part—which is the part that you and, especially, Pavel Anossov answered. – abarnert Mar 20 '13 at 21:40

4 Answers4

79

It slices the string to omit the last character, in this case a newline character:

>>> 'test\n'[:-1]
'test'

Since this works even on empty strings, it's a pretty safe way of removing that last character, if present:

>>> ''[:-1]
''

This works on any sequence, not just strings.

For lines in a text file, I’d actually use line.rstrip('\n') to only remove a newline; sometimes the last line in the file doesn’t end in a newline character and using slicing then removes whatever other character is last on that line.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
16

It means "all elements of the sequence but the last". In the context of f.readline()[:-1] it means "I'm pretty sure that line ends with a newline and I want to strip it".

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • 1
    Although Martijn Pieters's answer is a bit more detailed and explanatory, I like that this one directly describes the idiom of using `[:-1]` with `readline`, answering the OP's "in the context of…" part. (So, +1 to both of you.) – abarnert Mar 20 '13 at 21:44
  • It might be worth noting that making this assumption can be a dangerous thing to do, even on a system where you expect the end-of-line marker to be one character long-- it's not uncommon to wind up with a missing newline on the last line of a file. This is particularly true when Python is involved: I've seen `'\n'.join(lines)` used before, which will result in exactly this happening, and then you lose a data character.. – DSM Mar 20 '13 at 21:44
15

It selects all but the last element of a sequence.

Example below using a list:

In [15]: a=range(10)

In [16]: a
Out[16]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [17]: a[:-1]
Out[17]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
3

It gets all the elements from the list (or characters from a string) but the last element.

: represents going through the list -1 implies the last element of the list

Kartik
  • 9,463
  • 9
  • 48
  • 52
  • 4
    This is written ambiguously enough to be confusing. It's all of the elements up to _but not including_ the last element. (If you already know how slices/ranges/etc. work in Python, then knowing that -1 is the last element tells you everything you need… but if you don't, you still won't after this answer.) – abarnert Mar 20 '13 at 21:42