Python Tips (iii)--list three ways to delete an element __python

Source: Internet
Author: User

We take a list of element types as an example, and we do the deletion of the listing elements:

>>> L = [' No surfing ', ' flippers ']
Law I: Remove (val)
>>> l.remove (' no surfing ')
>>> l
[' flippers ']
(1) The parameter of remove () is a specific element value, not an index,

(2) If you know the index, how to remove the element values on the index using remove,

>>> L.remove (l[1])

This is a great risk because list allows duplicates, remove () deletes the first and the parameter values equal to the element method two: Pop (0)

(1) Pop () receives the index, without the parameter deletion is the last element (typical stack characteristics )

(2) a pop () has a return value that returns the deleted element value

L.pop () <==> L.pop (-1)

(3) The Append () (added to the tail) of the list, pop () (ejected from the tail), succeeded in turning the list into stack method three: Del (l[0])

>>> del (l[0])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.