Noob Python Question: List Confusion

Posted by potatocubed on Stack Overflow See other posts from Stack Overflow or by potatocubed
Published on 2010-03-29T21:51:47Z Indexed on 2010/03/29 22:03 UTC
Read the original article Hit count: 145

Filed under:
|

I'm trying to transfer the contents of one list to another, but it's not working and I don't know why not. My code looks like this:

list1 = [1, 2, 3, 4, 5, 6]
list2 = []

for item in list1:
    list2.append(item)
    list1.remove(item)

But if I run it my output looks like this:

>>> list1
[2, 4, 6]
>>> list2
[1, 3, 5]

My question is threefold, I guess: Why is this happening, how do I make it work, and am I overlooking an incredibly simple solution like a 'move' statement or something?

© Stack Overflow or respective owner

Related posts about python

Related posts about lists