[Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
Kent Johnson
kent37 at tds.net
Sun Feb 15 21:06:24 CET 2009
On Sun, Feb 15, 2009 at 2:47 PM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> I'm still looking for an explanation of "for (line_cnt, each_line) in
> enumerate(input_file)". Why the tuple? Apparently, line_count gets a line
> number, and each_line gets the string of text.
Do you know about sequence unpacking? In an assignment statement, when
the right side is a sequence, the left side can be a list of variables
of the same length as the sequence. Then each sequence element is
assigned to one variable. For example,
In [24]: item = (0, 'Spring')
In [25]: i, season = item
In [26]: i
Out[26]: 0
In [28]: season
Out[28]: 'Spring'
This can be used in a for loop, too, if the items are sequences. That
is what is commonly done with enumerate().
Kent
More information about the Tutor
mailing list