"For" loop first iteration

Question:

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)
Asked By: Rince

||

Answers:

Can’t you do root.copy(something.get()) before the loop?

EDIT: Sorry, I missed the second bit. But you get the general idea. Otherwise, enumerate and check for 0?

EDIT2: Ok, got rid of the silly second idea.

Answered By: Skilldrick

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.

Answered By: Daniel Bruce

This works:

for number, member in enumerate(something.get()):
    if not number:
        root.copy(member)
    else:
        somewhereElse.copy(member)
    foo(member)

In most cases, though, I’d suggest just iterating over whatever[1:] and doing the root thing outside the loop; that’s usually more readable. Depends on your use case, of course.

Answered By: balpha

If something.get() iterates over something, you can do it also as follows:

root.copy(something.get())

for member in something.get():
  #  the rest of the loop
Answered By: Eli Bendersky

You have several choices for the Head-Tail design pattern.

seq= something.get()
root.copy( seq[0] )
foo( seq[0] )
for member in seq[1:]:
    somewhereElse.copy(member)
    foo( member )

Or this

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?

Answered By: S.Lott

Here, I could come with a Pythonic idiom that can look “pertty”. Although, most likely I’d use the form you suggested in asking the question, just for the code to remain more obvious, though less elegant.

def copy_iter():
    yield root.copy
    while True:
        yield somewhereElse.copy

for member, copy in zip(something.get(), copy_iter()):
    copy(member)
    foo(member)

(sorry – the first I posted, before editing, form would not work, I had forgotten to actually get an iterator for the ‘copy’ object)

Answered By: jsbueno

I think this is quite elegant, but maybe too convoluted for what it does…

from itertools import chain, repeat, izip
for place, member in izip(chain([root], repeat(somewhereElse)), something.get()):
    place.copy(member)
    foo(member)
Answered By: fortran

How about using iter, and consuming the first element?

Edit: Going back on the OP’s question, there is a common operation that you want to perform on all elements, and then one operation you want to perform on the first element, and another on the rest.

If it’s just a single function call, I’d say just write it twice. It won’t end the world. If it’s more involved, you can use a decorator to wrap your “first” function and “rest” function with a common operation.

def common(item):
    print "common (x**2):", item**2

def wrap_common(func):
    """Wraps `func` with a common operation"""
    def wrapped(item):
        func(item)
        common(item)
    return wrapped

@wrap_common
def first(item):
    """Performed on first item"""
    print "first:", item+2

@wrap_common
def rest(item):
    """Performed on rest of items"""
    print "rest:", item+5

items = iter(range(5))
first(items.next())

for item in items:
    rest(item)

Output:

first: 2
common (x**2): 0
rest: 6
common (x**2): 1
rest: 7
common (x**2): 4
rest: 8
common (x**2): 9
rest: 9
common (x**2): 16

or you could do a slice:

first(items[0])
for item in items[1:]:
    rest(item)
Answered By: Ryan Ginstrom

I don’t know Python, but I use almost the exact pattern of your example.
What I do also is making the if condition the most frequent, so usually check for if( first == false )
Why? for long loops, first will be true only one time and will be false all the other times, meaning that in all loops but the first, the program will check for the condition and jump to the else part.
By checking for first being false, there will be only one jump to the else part. I don’t really know if this adds efficiency at all, but I do it anyway, just to be in peace with my inner nerd.

PS: Yes, I know that when entering the if part, it also has to jump over the else to continue execution, so probably my way of doing it is useless, but it feels nice. 😀

Answered By: Petruza

I think the first S.Lott solution is the best, but there’s another choice if you’re using a pretty recent python (>= 2.6 I think, since izip_longest doesn’t seem available before that version) that lets doing different things for the first element and successive one, and can be easily modified to do distinct operations for 1st, 2nd, 3rd element… as well.

from itertools import izip_longest

seq = [1, 2, 3, 4, 5]

def headfunc(value):
    # do something
    print "1st value: %s" % value

def tailfunc(value):
    # do something else
    print "this is another value: %s" % value

def foo(value):
    print "perform this at ANY iteration."

for member, func in izip_longest(seq, [headfunc], fillvalue=tailfunc):
    func(member)
    foo(member)
Answered By: Alan Franzoni

how about:

my_array = something.get()
for member in my_array:
    if my_array.index(member) == 0:
        root.copy(member)
    else:
        somewhereElse.copy(member)
    foo(member)

or maybe:

for index, member in enumerate(something.get()):
    if index == 0:
        root.copy(member)
    else:
        somewhereElse.copy(member)
    foo(member)

Documentation of index-method.

Answered By: dummy

Your question is contradictory. You say “only do something on first iteration”, when in fact you are saying do something different on first/subsequent iterations. This is how I would attempt it:

copyfn = root.copy
for member in something.get():
    copyfn(member)
    foo(member)
    copyfn = somewhereElse.copy
Answered By: aaa90210

Here is what works for me

    dup_count = 0
    for x in reversed(dup_list):
        dup_count += 1
        if dup_count == 1:
            print("First obj {}: {}".format(dup_count,x))
        else:
            print("Object # {}:  {}".format( dup_count,x  ))
Answered By: Operation420.net

With a helper that only runs for the first member and then replaces itself appropriately:

def copy(member):
    root.copy(member)
    global copy
    copy = somewhereElse.copy

for member in something.get():
    copy(member)
    foo(member)

(If your loop is inside a function, replace global with nonlocal.)

Answered By: Kelly Bundy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.