Moving items from list to list

Evan Klitzke evan at yelp.com
Thu Jun 14 13:23:14 EDT 2007


On 6/14/07, HMS Surprise <john at datavoiceint.com> wrote:
>
> Just wondered if there was some python idiom for moving a few items
> from one list to another. I often need to delete 2 or 3 items from one
> list and put them in another. Delete doesn't seem to have a return
> value. I don't care which items I get so now I just use a couple of
> pops or a for loop for more than two.

I'm not sure if this is what you're asking, but if the elements in the
list are contiguous you can just use list slicing/addition, like this:

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]

b = a[2:] + b
a = a[:2]

Now the contents of a and b respectively are a = [1, 2] and b = [3, 4,
5, 6, 7, 8, 9, 10].

-- 
Evan Klitzke <evan at yelp.com>



More information about the Python-list mailing list