Moving items from list to list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HMS Surprise

    Moving items from list to list


    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.

    Thanks

    jh

  • Evan Klitzke

    #2
    Re: Moving items from list to list

    On 6/14/07, HMS Surprise <john@datavoice int.comwrote:
    >
    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@yelp.co m>

    Comment

    • HMS Surprise

      #3
      Re: Moving items from list to list

      Thanks.

      That will work. The 2nd, smaller lst starts out empty but this is
      easily adapted.

      jh

      Comment

      • George Sakkis

        #4
        Re: Moving items from list to list

        On Jun 14, 12:30 pm, HMS Surprise <j...@datavoice int.comwrote:
        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.
        >
        Thanks
        >
        jh
        >>x = range(10)
        >>y = []
        >>y.append(x.po p(4))
        >>print x, y
        [0, 1, 2, 3, 5, 6, 7, 8, 9] [4]
        >>y.append(x.po p(7))
        >>print x, y
        [0, 1, 2, 3, 5, 6, 7, 9] [4, 8]


        HTH,
        George

        Comment

        • Gabriel Genellina

          #5
          Re: Moving items from list to list

          En Thu, 14 Jun 2007 14:23:14 -0300, Evan Klitzke <evan@yelp.come scribió:
          On 6/14/07, HMS Surprise <john@datavoice int.comwrote:
          >>
          >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].
          When lists become large this is less convenient because it has to build
          three intermediate lists. At least on my box, pop+append is 5 orders of
          magnitude faster (but as shown on another posts, you should test on your
          box to see what happens):

          c:\temp>call python -m timeit -s "a=range(100000 0);b=range(1000 )"
          "b.append(a .po
          p())"
          1000000 loops, best of 3: 1.35 usec per loop

          c:\temp>call python -m timeit -s "a=range(100000 0);b=range(1000 )"
          "b+=a[-1:];a=a
          [:-1]"
          10 loops, best of 3: 107 msec per loop

          c:\temp>call python -m timeit -s "a=range(100000 0);b=range(1000 )"
          "b.append(a[-1
          ]);a=a[:-1]"
          10 loops, best of 3: 107 msec per loop

          c:\temp>call python -m timeit -s "a=range(100000 0);b=range(1000 )"
          "b.append(a[0]
          );a=a[1:]"
          10 loops, best of 3: 110 msec per loop

          --
          Gabriel Genellina

          Comment

          Working...