Sorting list (maybe stupid)

Russell Blau russblau at
Tue Jun 11 18:05:43 CEST 2002

"Max M" <maxm at> wrote in message news:3D05F3FD.7020202 at
> afile = open('myfile.txt')
> tuplelist = []
> for aline in afile:
>      nummericValue = int(aline[:aline.find('.')])
>      tuplelist.append((nummericValue, aline))
> tuplelist.sort()
> sortedLines = [tup[1] for tup in tuplelist]
> for line in sortedLines:
>      print line

If the programmer is *certain* that each line will be in the format
given by the OP, which is a line number followed by a period and a
space, followed by arbitrary text, then the solution can be even

linelist = []
for aline in afile:
    linelist.append(aline.split(".", 1))
# each entry in linelist is a list of two elements: [line_number,
resultlist = []
for item in linelist:
# used resultlist in case the OP wants to do something with these lines
besides just print them
# alternately, you could join the strings in-place in linelist
for line in resultlist:
    print line

I don't actually have a hotmail account; but I do have one on
if you really want to get in touch with me.

More information about the Python-list mailing list