[Tutor] Accessing next and previous items during iteration

Ed Singleton singletoned at gmail.com
Fri Dec 16 11:10:30 CET 2005


Is it possible to access the next and previous items during an iteration?

I want to use it to iterate through html files in a folder and add
links in to the next and previous pages.

For example

for page in folder:
    #add link to previous page
    #add link to next page

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

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).

Is there a nice way to do it?

Thanks

Ed


More information about the Tutor mailing list