Change one list item in place

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Dec 1 03:54:49 EST 2010


On Tue, 30 Nov 2010 17:08:57 -0800, Gnarlodious wrote:

> This works for me:
> 
> def sendList():
>     return ["item0", "item1"]
> 
> def query():
>     l=sendList()
>     return ["Formatting only {0} into a string".format(l[0]), l[1]]


For the record, you're not actually changing a list in place, you're 
creating a new list. 

I would prefer:

def query():
    l = sendList()
    l[0] = "Formatting only {0} into a string".format(l[0])
    return l


which will continue to work even if sendlist() gets changed to return 47 
items instead of 2.

An alternative would be:



-- 
Steven



More information about the Python-list mailing list