[Tutor] Replacement of items in a list

Andrei Kulakov ak@silmarill.org
Fri, 03 Aug 2001 14:44:47 -0400


On Fri, Aug 03, 2001 at 12:30:52PM -0600, VanL wrote:
> Why is this?
> 
> :::::::::::::: Begin test.py
> 
> #!/usr/bin/env python
> 
> import string
> 
> lst = ['ONE', 'TWO', 'THREE']
> lst1 = lst[:]
> lst2 = lst[:]
> 
> def replace1(list):
>     """ Uses item replacement"""
>     for x in list:
>         x = string.lower(x)

This doesn't change values in list. for x in list: iterates over
newly created values that it takes from list. Here's how I'd do it (2.0+):

>>> lst
['ONE', 'TWO', 'THREE']
>>> newlst = [x.lower() for x in lst]
>>> newlst
['one', 'two', 'three']

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline