Ed Singleton wrote: > Is it possible to access the next and previous items during an iteration?
There is nothing built in to support this directly. > I'm currently using: > > prev = 0 > current = 0 > next = 0 > for page in folder: > prev = current > current = next > next = page > if current: > if prev: > #add link to previous page > #add link to next page > if current: > if prev: > #add link to previous page > #add link to next page I think there is a bug here - when the loop exits, prev, current and next will all be valid with next containing the last page. You have already added the links to current, it is next that needs a link to the previous page. > > But this seems a really awkward way to do it. > > I've considered iterating and dumping them all into a list and then > iterating through the list, but that also seems awkward (having to > iterate twice). You don't say what kind of object folder is. If it is a directory listing e.g. from os.listdir() then it is already a list. In any case you should be able to make a list without an explicit iteration using pages = list(folder) > > Is there a nice way to do it? How about this: pages = list(folder) # make sure we have a list for i, page in enumerate(pages): if i > 0: previous = pages[i-1] # add link to previous if i+1 < len(pages): next = pages[i+1] # add link to next Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor