[Tutor] Can someone please help me with this?

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Nov 6 10:52:32 CET 2013


On 6 November 2013 03:10, Anton Gilb <antongilb at gmail.com> wrote:
> Hi everyone, I'm a novice student struggling through my first python course.
> Currently stuck on how to tackle this exercise. Any input would be
> appreciated, and I know you guys can not just give me the answer, I'm just
> looking for help to understand how to work through this.

That's fine.

> This is the exercise:
>
> It is often adventageous to be able to transfere data between multiple lists
> while rearranging their order. For instance, say that list1 =
> [1,2,3,4,5,6,7,8,9] and you wish to add the numbers in the index range 4:7
> of list1 to another list, list2, in reverse order while simulataneously
> removing them from list1. If list2 = [100,200], the result will be list2 =
> [100,200,7,6,5]. 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).
>
> This is what I've come up with so far, it looks like garbage I know.
>
> list1 = [1,2,3,4,5,6,7,8,9]
> list2 = [100,200]
> final_list2 = []
>
> def transform(list1, list2, r1, r2):
>         temp_list = list1[4:7:-1]
>         final_list2 = list2.append(temp_list)
>
>
> print(final_list2)

Which part don't you get? You should say what the code does, why it's
not what you want it to do and whether or not you understand the
behaviour you see.

I'll give one suggestion which is that to concatenate one list onto
the end of another you would use the .extend() method rather than the
.append() method.


Oscar


More information about the Tutor mailing list