Order guaranteed with list comprehension?

Terry Reedy tjreedy at udel.edu
Fri May 16 13:09:20 EDT 2003


"Harald Massa" <cpl.19.ghum at spamgourmet.com> wrote in message
news:Xns937D7008473Fcpl19ghumspamgourmet at 62.153.159.134...
>
> >>> maedels=['Sandra', 'Miriam', 'Katja', 'Melanie']
> >>> my=[a for a in maedels if a[0]=="M"]

This is defined to be sematically identical to

my = []
for a in maedel:
    if [0] == "M":
        my.append[a]


> >>> my
> ['Miriam', 'Melanie']
>
> I select part of a list ... the names starting with "M"
>
> the comprehended list is in the same order as the basic list.
>
> Is this guaranteed by specification

See spec above.  (Yes, for statements scan in order by spec.)

Terry J. Reedy






More information about the Python-list mailing list