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

From: Jordan P
Sent on: Friday, August 13, 2010 11:22 PM
I do understand how list.insert() edits the list in place. This doesn't mean it shouldn't return the list after it's done.

I realize you could easily get around the limitation by defining a function as follows:

def insertTo(list, val):
?? ?list.insert(0, val)
?? ?return val

to be used like

map(lambda i: insertTo(i, "0"), listOfLists)

I don't need help getting it to work.. I want to know why Python doesn't do this automatically. Why the decision was made to not return anything from list methods. Because honestly, I can't imagine why they do not.

--jordan

On Fri, Aug 13, 2010 at 11:06 PM, James Dennis <[address removed]> wrote:
First, you need access to the list before you call one of it's functions and second, insert works in place, so if you have list x and call x.insert(...), x will reflect that change right away.

>>> x = ['a','b','c']
>>> x.append('d')
>>> l
['a', 'b', 'c', 'd']
>>> x.insert(0, 'z')
>>> x
['z', 'a', 'b', 'c', 'd']
>>>?


On Fri, Aug 13, 2010 at 11:01 PM, Jordan P <[address removed]> 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])
This message was sent by Jordan P ([address removed]) from The New York Python Meetup Group.
To learn more about Jordan P, visit his/her member profile
To unsubscribe or to update your mailing list settings, click here

Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by James Dennis ([address removed]) from The New York Python Meetup Group.
To learn more about James Dennis, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

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