string, split, sort, None, huh?

Jay O'Connor joconnor at cybermesa.com
Sat Feb 17 00:01:35 CET 2001


Phlip wrote:

> Thy Pon:
>
> Gonna split a document by linefeeds into strings, then sort the strings.
> Child's play, huh?
>
>         lines = split ( """Mary
> had
> a
> little
> lamb""", "\n" )
>
>         print repr(lines)
>         print repr(lines.sort())
>
> Now the output:
>
>         ['Mary', 'had', 'a', 'little', 'lamb']
>         None
>
> So where'd that 'None' come from?

It came from the fact that  lines.sort() sorts the sequence in in place and
returns None, which is what your second repr() is getting

Try instead:

    print repr (lines)
    lines.sort()
    print repr(lines)

Take care,
Jay O'Connor
joconnor at cybermesa.com




More information about the Python-list mailing list