[Tutor] Can I tell me anyone about [x.lower() for x in lst]
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 3 Sep 2001 12:34:25 +0100
> >>> lst =3D ['ONE', 'TWO', 'THREE']
> >>> newlst =3D [x.lower() for x in lst]
> File "<stdin>", line 1
> newlst =3D [x.lower() for x in lst]
>
> I am using python1.5 on Red Hat Linux 7.1.
> Does the facility "[x.lower() for x in lst]" not
> available in python1.5 ?
Correct. List comprehensions were introduced in version 2.0.
The equivalent code for 1.5 is:
lst = ['ONE', 'TWO', 'THREE']
newlst = map(string.lower, lst)
OR without a functional approach:
lst = ['ONE', 'TWO', 'THREE']
newlst = []
for x in lst:
newlst.append(string.lower(x))
Alan g