3

I have the next part of code in python:

for (script, location) in self.device.scripts:

How can I start to take elements from the second pair of the given list? And if that is possible where should I check if a second element exist?

JoeDonald
  • 125
  • 3
  • 12

2 Answers2

7

Use itertools.islice() to slice skipping the first.

from itertools import islice
for (script, location) in islice(self.device.scripts, 1, None):
    pass # do stuff
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
7

Just slice it.

for script, location in self.device.scripts[1:]:
    pass

For your second question, you don't need to worry about any IndexError since slicing returns an empty list when it's out of range.

Taku
  • 31,927
  • 11
  • 74
  • 85