0

I'm trying to reduce an important list of list in python as I don't need as much data to get where i want to go, ie drawing a route

mylist = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14], 
          [15, 16], [17, 18], [19, 20], [21, 22], [23, 24]]

I would like to "jump" some item of the list to reduce the size. Example below where i skip 2 items every 3 lists.

mylist = [[1, 2], [7, 8], [13, 14], [19, 20]]

I tried with mylist[1::3] but wasn't successful

Thanks.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172

2 Answers2

1

Your starting index was wrong - Python list indices start from 0. mylist[0::3], or equivalently mylist[::3], works just fine:

>>> mylist = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14], [15, 16], [17, 18], [19, 20], [21, 22], [23, 24]]

>>> mylist[::3]
[[1, 2], [7, 8], [13, 14], [19, 20]]
meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
0
mylist[::3]

works for me.

mylist[1::3]

goes to the first element and skips to every third after that, giving:

[[3, 4], [9, 10], [15, 16], [21, 22]]
Kaiwen Chen
  • 192
  • 1
  • 1
  • 12