Program in python 3.x. Please add comments in order to increase understanding of
ID: 3872202 • Letter: P
Question
Program in python 3.x. Please add comments in order to increase understanding of the solution. (If possible try to make the code as simple as possible). Thanks
It is oftentimes advantageous to be able to transfer 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 listl to another list named listz, in reverse order while simultaneusly removing them from Iistl. I list2 = [100.200] , the result will be list2 = [100, 200, 7,6,5]. Write a function named transform that takes as arguments listl, list2,rl, and r2, that removes items from listl in the slice rl: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)Explanation / Answer
def transform(list1,list2,r1,r2) : #declaring the function transform
res = list1[r1:r2] #taking a slice from the first list and storing in another list
res.reverse() #reversing the obtained list
list2 += res #concating the string to existing list2
return list2 #returning list
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [10,20]
list2 = transform(list1,list2,4,7) #calling the transform function
print(list2)
output:
[10, 20, 7, 6, 5]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.