Best way to check that you are at the beginning (the end) of an iterable?

Laurent laurent.payot at gmail.com
Wed Sep 7 23:35:41 CEST 2011


Hi there,

What is the simplest way to check that you are at the beginning or at the end of an iterable? I'm using enumerate with Python 3.2 (see below) but I'm wondering if there would be a better way.


l = ['a', 'b', 'a', 'c']

for pos, i in enumerate(l):
    if pos == 0:
        print("head =", i)
    else:
        print(i)


I know that Python is not exactly a functional language but wouldn't something like "ishead()" or "istail()" be useful?




More information about the Python-list mailing list