2

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
  • 115
  • 3
  • 11

2 Answers2

6

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
  • 28,570
  • 11
  • 65
  • 77
5

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
  • 121,762
  • 30
  • 236
  • 257