On Tue, Nov 5, 2013 at 10:10 PM, Anton Gilb <antong...@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)) _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor