0

Below you can see the sequence commands which simply add elements from the dirs list to some other list new using special built-in function:

new = Trajectory(os.path.join(path, dirs[0]))
new.addFile(os.path.join(path, dirs[1]))
new.addFile(os.path.join(path, dirs[2]))
new.addFile(os.path.join(path, dirs[3]))

I need to simplify this script placing all new.addFile in a loop like:

for element in dirs:
 new.addFile(os.path.join(path, element)

The question: how to add some rule which exclude dirs[0] to adding using this loop on the first step? In my case dirs[0] is already always present within new so I need not to add it again while looping the first list.

user3470313
  • 303
  • 5
  • 16

3 Answers3

3

You can simply slice the dirs list to skip the first item:

for element in dirs[1:]:

Demo:

>>> lst = [1, 2, 3, 4]
>>> for i in lst[1:]:
...     i
...
2
3
4
>>>
Community
  • 1
  • 1
1

this is really straightforward:

for element in dirs[1:]:
    ...
acushner
  • 9,595
  • 1
  • 34
  • 34
1

Try this:

for i in range(1, len(dirs)):
    new.addFile(os.path.join(path, dirs[i])

This just starts the loop at 1 rather than 0.

Benjamin James Drury
  • 2,353
  • 1
  • 14
  • 27