Re: Change one list item in place

2010-12-01 Thread Steven D'Aprano
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Change one list item in place

2010-12-01 Thread Steven D'Aprano
On Wed, 01 Dec 2010 08:54:49 +, Steven D'Aprano wrote:

 An alternative would be:


Please ignore. That was an accidental Send mid-edit.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Change one list item in place

2010-12-01 Thread Jean-Michel Pichavant

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?

-- Gnarlie
  

what about

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



JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: Change one list item in place

2010-12-01 Thread Gnarlodious
On Dec 1, 6:23 am, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Change one list item in place

2010-12-01 Thread Jean-Michel Pichavant

Gnarlodious wrote:

On Dec 1, 6:23 am, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:
  

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






--
http://mail.python.org/mailman/listinfo/python-list


Re: Change one list item in place

2010-12-01 Thread Steven D'Aprano
On Wed, 01 Dec 2010 05:33:55 -0800, Gnarlodious wrote:

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

You make it sound like that's an onerous task.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Change one list item in place

2010-11-30 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Change one list item in place

2010-11-30 Thread MRAB

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. :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Change one list item in place

2010-11-30 Thread Gnarlodious
Thanks.
Unless someone has a simpler solution, I'll stick with 2 lines.

-- Gnarlie
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Change one list item in place

2010-11-30 Thread Steve Holden
On 11/30/2010 8:28 PM, MRAB wrote:
 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. :-)

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
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list