Paul has just turned away in a few days, so how do we remove Paul from the existing list?
If Paul is ranked last, we can
pop()delete it by list :
>>> L = ['Adam', 'Lisa', 'Bart', 'Paul'] >>> L.pop() 'Paul' >>> print L ['Adam', 'Lisa', 'Bart']
The pop() method always deletes the last element of the list, and it also returns this element, so after we execute L.pop(), we print out ‘Paul’.
What if Paul is not in the last place? For example, Paul is ranked third:
>>> L = ['Adam', 'Lisa', 'Paul', 'Bart']
To kick Paul out of the list, we must first locate Paul’s position. Since Paul’s index is 2, use
pop(2)Paul to delete:
>>> L.pop(2) 'Paul' >>> print L ['Adam', 'Lisa', 'Bart']
Note that the list in the editor code on the right is as follows:
L = [‘Adam’, ‘Lisa’, ‘Paul’, ‘Bart’]
Paul’s index is 2, Bart’s index is 3, if we want to delete both Paul and Bart, please explain why the following code does not work correctly:
How to adjust the code to delete both Paul and Bart correctly?