[Tutor] How do I (idiomatically) determine when I'm looking at the last entry in a list?

Flynn, Stephen (L & P - IT) Steve.Flynn at capita.co.uk
Wed Oct 28 10:48:05 EDT 2015


Afternoon,

	Python 3.

	I'm iterating through a list and I'd like to know when I'm at
the end of the said list, so I can do something different. For example

list_of_things = ['some', 'special', 'things']
for each_entry in list_of_things:
	print(each_entry)
	if each_entry == list_of_things[-1]: # do something special to
last entry
	...etc


Is this the idiomatic way to detect you're at the last entry in a list
as you iterate through it?



For context, I'm working my way through a (csv) file which describes
some database tables. I'm building the Oracle DDL to create that table
as I go. When I find myself building the last column, I want to finish
the definition with a ");" rather than the usual "," which occurs at the
end of all other column definitions...

e.g.
CREATE TABLE wibble
(
	Col1	CHAR(2),
	Col2	NUMBER(5,2),
);

Regards,

Steve.


This email is security checked and subject to the disclaimer on web-page: http://www.capita.co.uk/email-disclaimer.aspx


More information about the Tutor mailing list