I would like to inquire if there is an elegant pythonic way of executing some function on the first loop iteration. The only possibility I can think of is:
first = True for member in something.get(): if first: root.copy(member) first = False else: somewhereElse.copy(member) foo(member)
You have several choices for the Head-Tail design pattern.
seq= something.get() root.copy( seq ) foo( seq ) for member in seq[1:]: somewhereElse.copy(member) foo( member )
seq_iter= iter( something.get() ) head = seq_iter.next() root.copy( head ) foo( head ) for member in seq_iter: somewhereElse.copy( member ) foo( member )
People whine that this is somehow not "DRY" because the "redundant foo(member)" code. That's a ridiculous claim. If that was true then all functions could only be used once. What's the point of defining a function if you can only have one reference?
Something like this should work.
for i, member in enumerate(something.get()): if i == 0: # Do thing # Code for everything
However, I would strongly recommend thinking about your code to see if you really have to do it this way, because it's sort of "dirty". Better would be to fetch the element that needs special handling up front, then do regular handling for all the others in the loop.
The only reason I could see for not doing it this way is for a big list you'd be getting from a generator expression (which you wouldn't want to fetch up front because it wouldn't fit in memory), or similar situations.