Mastiff
Mastiff

Reputation: 2240

Python numpy array negative start index with colon notation

Suppose I have an array like:

a = np.arange(0,10)

Why does a[-1:9] give an empty result? I expected it to give a result containing a[-1], a[0], a[1], ... a[8].

Upvotes: 0

Views: 1348

Answers (1)

chepner
chepner

Reputation: 531325

The slice is interpreted as starting at a[-1], which is the same as a[len(a)-1], so a[-1:9] is equivalent to a[9:9], which is an empty list. Your expected result isn't a contiguous range, which is what a slice must produce.

Upvotes: 1

Related Questions