0

This is a rather basic question as I am new to Python coming from R.

In R we can access non consecutive slices in a vector or list like this:

> a = 1:10
> a[c(2:4, 7:9)]
[1] 2 3 4 7 8 9
> list_a = list(1:10)
> list_a[[1]][c(2:4, 7:9)]
[1] 2 3 4 7 8 9

I am trying to find out how to do the same with a list in Python.

E.g.

a = list(range(20))
a[1:4]
# returns
[1, 2, 3]
# but the following syntax creates an exception:
a[1:4, 7:9]

Your advice will be appreciated.

rf7
  • 1,993
  • 4
  • 21
  • 35
  • 2
    some suggestions will be found here : https://stackoverflow.com/questions/22412509/getting-a-sublist-of-a-python-list-with-the-given-indices , I particularly like the use of operator.itemgetter which seems to be the most efficient – WNG Nov 24 '17 at 23:22

1 Answers1

1

You could accomplish what you want in a[1:4,7:9] by using a[1:4]+a[7:9].

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101