Question: Modifying Lists For this question, in chop.py you will create two similar functions that actually work very differently. Both of these functions will be used

Modifying Lists
For this question, in chop.py you will create two similar functions that actually work very differently. Both of these functions will be used to remove the last item from a list, but will operate in different ways.
Write a function chop1 that returns its argument with the last item removed. It should not change the argument, but should return a copy without the last item.
>>> testlist =[0,1,2,3,4]
>>> chop1(testlist) # note: returns modified copy
[0,1,2,3]
>>> print(testlist) # note: original unchanged
[0,1,2,3,4]
Write a function chop2 that removes the last item from a list given as its argument. The existing list should be changed in-place; the function should not return anything.
>>> testlist =[0,1,2,3,4]
>>> chop2(testlist) # note: no return value
>>> print(testlist)
[0,1,2,3]
The difference between these two functions is important. The first operates like all of the other functions we've written: taken arguments and returned whatever results it created. The second is possible because lists are mutable: the function takes the reference to the list and changes it in-place.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!