Change one list item in place

MRAB python at mrabarnett.plus.com
Tue Nov 30 20:28:11 EST 2010


On 01/12/2010 01:08, 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]]
>
> query()
>
>
> However, is there a way to bypass the
>
> l=sendList()
>
> and change one list item in-place? Possibly a list comprehension
> operating on a numbered item?
>
There's this:

     return ["Formatting only {0} into a string".format(x) if i == 0 
else x for i, x in enumerate(sendList())]

but that's too clever for its own good. Keep it simple. :-)



More information about the Python-list mailing list