Getting two python (List and Tuple type) [2-5 pythonlist delete new elements]

2-5 pythonlist delete new elements

 

Paul had just arrived a few days the students have to turn away, then how do we put Paul deleted from the existing list in it?

 

If Paul students came in last, we can list thepop() delete method:

1 >>> L = ['Adam', 'Lisa', 'Bart', 'Paul']
2 >>> L.pop()
3 'Paul'
4 >>> print L
5 ['Adam', 'Lisa', 'Bart']

pop () method always delete the list of the last element after, and it returns this element, so we execute L.pop (), prints out 'Paul'.

If Paul is not ranked in the students how to do the last one? For example, Paul ranked third classmate:

1 >>> L = ['Adam', 'Lisa', 'Paul', 'Bart']

Paul put kicked list, we must first locate the position of Paul. Because of Paul's index is 2, therefore, with  pop(2)the Paul deleted:

1 >>> L.pop(2)
2 'Paul'
3 >>> print L
4 ['Adam', 'Lisa', 'Bart']

 

Guess you like

Origin www.cnblogs.com/ucasljq/p/11585853.html