[Tutor] Can someone please help me with this?

eryksun eryksun at gmail.com
Wed Nov 6 16:15:38 CET 2013


On Tue, Nov 5, 2013 at 10:10 PM, Anton Gilb <antongilb at gmail.com> wrote:
> Write a function named transform that takes as arguments
> list1, list2, r1, and r2, that removes items from list1 in the slice r1:r2,
> appends them onto list2 in reverse order, and returns the resulting list.
> For example, in this case, the function call will be transform(list1, list2,
> 4,7).
>
> list1 = [1,2,3,4,5,6,7,8,9]
> list2 = [100,200]

For the slice 4 to 7, the reversed slice is 6 down to 3.

    list2 += list1[6:3:-1]

    >>> list2
    [100, 200, 7, 6, 5]

Then del the slice from list1:

    del list1[4:7]

    >>> list1
    [1, 2, 3, 4, 8, 9]

Alternatively you could use pop() and append() in a loop over the
reversed range, though it's not as efficient:

    for i in xrange(6, 3, -1):
        list2.append(list1.pop(i))


More information about the Tutor mailing list