Re: [nycpython] question about list.insert()

From: Eric S.
Sent on: Saturday, August 14, 2010 4:30 AM
It's a design decision that mutating methods do not return self. It's so 
that you're forced to deal with them as mutators, and you don't 
accidentally (or through ignorance) assume they return a new list.

If you want to work around this, you can either create helper functions, 
or get more elaborate, such as this example:­


On 8/13/10 11:00 PM, Jordan P wrote:
> hey nycpython,
> This is my first post to the group, though I've been a member for a while!
> I came across an interesting quirk in standard python regarding lists. I
> was in the middle of writing a beastly lambda expression and found
> myself having to insert a value at the beginning of a list. The design
> of my expression required that whatever call I made would have to return
> the list so that it could be used further up the chain.
> looking at the docs, I saw that list.insert(0, value) returns None, not
> the list.
> I was stuck for a good half hour until someone suggested I use: [val] +
> list instead. They warned, however, that this expression will cause a
> copy of list to be returned, not the original list. This stinks for
> efficiency.
> Can anybody come up with a reason why python's list methods (append,
> insert, extend, etc..) do not return the list? I'm using [val]+list for
> now, but would much rather not be making copies of all my lists.
> Looking forward to some interesting discussion--
> --Jordan
> --
> Please Note: If you hit "*REPLY*", your message will be sent to
> *everyone* on this mailing list ([address removed]
> <mailto:[address removed]>)
> This message was sent by Jordan P ([address removed]) from The New
> York Python Meetup Group <http://www.meetup...;­.
> To learn more about Jordan P, visit his/her member profile
> <http://www.meetup...;­
> To unsubscribe or to update your mailing list settings, click here
> <http://www.meetup...;­
> Meetup, PO Box 4668 #37895 New York, New York[masked] |
> [address removed]


Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy