What is the pythonic way to detect the last element in a 'for' loop? What is the pythonic way to detect the last element in a 'for' loop? python python

What is the pythonic way to detect the last element in a 'for' loop?


Most of the times it is easier (and cheaper) to make the first iteration the special case instead of the last one:

first = Truefor data in data_list:    if first:        first = False    else:        between_items()    item()

This will work for any iterable, even for those that have no len():

file = open('/path/to/file')for line in file:    process_line(line)    # No way of telling if this is the last line!

Apart from that, I don't think there is a generally superior solution as it depends on what you are trying to do. For example, if you are building a string from a list, it's naturally better to use str.join() than using a for loop “with special case”.


Using the same principle but more compact:

for i, line in enumerate(data_list):    if i > 0:        between_items()    item()

Looks familiar, doesn't it? :)


For @ofko, and others who really need to find out if the current value of an iterable without len() is the last one, you will need to look ahead:

def lookahead(iterable):    """Pass through all values from the given iterable, augmented by the    information if there are more values to come after the current one    (True), or if it is the last value (False).    """    # Get an iterator and pull the first value.    it = iter(iterable)    last = next(it)    # Run the iterator to exhaustion (starting from the second value).    for val in it:        # Report the *previous* value (more to come).        yield last, True        last = val    # Report the last value.    yield last, False

Then you can use it like this:

>>> for i, has_more in lookahead(range(3)):...     print(i, has_more)0 True1 True2 False


Although that question is pretty old, I came here via google and I found a quite simple way: List slicing. Let's say you want to put an '&' between all list entries.

s = ""l = [1, 2, 3]for i in l[:-1]:    s = s + str(i) + ' & 's = s + str(l[-1])

This returns '1 & 2 & 3'.


if the items are unique:

for x in list:    #code    if x == list[-1]:        #code

other options:

pos = -1for x in list:    pos += 1    #code    if pos == len(list) - 1:        #codefor x in list:    #code#code - e.g. print xif len(list) > 0:    for x in list[:-1]        #code    for x in list[-1]:        #code