25

I want to use array slicing to trim my array i.e.

a_trimmed = a[trim_left:-trim_right]

this is great, except if trim_right is 0, I get a[trim_left:0], which is an empty array.

I suppose I can could it to

a[trim_left:a.shape[0]-trim_right]

but it's uglier. What's the cleanest way to express this?

bnjmn
  • 4,508
  • 4
  • 37
  • 52
so12311
  • 4,179
  • 1
  • 29
  • 37

2 Answers2

42

None is a valid slice endpoint:

a[trim_left:-trim_right or None]
lanzz
  • 42,060
  • 10
  • 89
  • 98
3

It's not that ugly IMHO. the only way I can come up with to make it shorter is replacing a.shape[0] with len(a)

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108