Change one list item in place

G

Gnarlodious

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?

-- Gnarlie
 
M

MRAB

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. :)
 
G

Gnarlodious

Thanks.
Unless someone has a simpler solution, I'll stick with 2 lines.

-- Gnarlie
 
S

Steve Holden

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. :)

I quite agree. That solution is so clever it would be asking for a fight
walking into a bar in Glasgow.

However, an unpacking assignment can make everything much more
comprehensible [pun intended] by removing the index operations. The
canonical solution would be something like:

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

regards
Steve
 
S

Steven D'Aprano

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:
 
J

Jean-Michel Pichavant

Gnarlodious said:
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?

-- Gnarlie
what about

def query():
return ["Formating only {0} into a string".format(sendList()[0])] +
sendList()[1:]


JM
 
G

Gnarlodious

what about

def query():
    return ["Formating only {0} into a string".format(sendList()[0])] +
sendList()[1:]

However this solution calls sendList() twice, which is too processor
intensive.

Thanks for all the ideas, I've resigned myself to unpacking a tuple
and reassembling it.

-- Gnarlie
http://Sectrum.com
 
J

Jean-Michel Pichavant

Gnarlodious said:
what about

def query():
return ["Formating only {0} into a string".format(sendList()[0])] +
sendList()[1:]

However this solution calls sendList() twice, which is too processor
intensive.
You got to get rid of those nerd habits of unecessary optimization. To
put it simple, you just don't give a [put whatever suitable word] to the
overhead of 2 calls of sendList instead of one.
Until query is called a million time a second, there's no need to
sacrifice anything on the optimization altar, because no one will never
ever see the difference.

You can find my solution not that readable, that would be a proper
reason for not using it. It's up to you.

JM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top