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

Source: Internet
Author: User

Let's take a list of elements of a string as an example, and delete the elements:

>>> l = [‘no surfing‘, ‘flippers‘]
    • 1
Law One: Remove (val)
>>> l.remove(‘no surfing‘)>>> l[‘flippers‘]
    • 1
    • 2
    • 3
    • (1) The parameter of remove () is a specific element value, not an index,
    • (2) If you know the index, how to use Remove to delete the element value on the index,

      l.remove(l[1])
      • 1

      This is a great risk because list allows duplicates, remove () delete the first element in the list and the value of the parameter equal

Law II: Pop (0)
    • (1) the pop () receives the index, and the last element (typical stack ) is deleted without reference.

    • (2) Pop () has a return value, which returns the value of the deleted element

      l.pop() <==> l.pop(-1)
      • 1
    • (3) List of append () (added to the tail), pop () (ejected from the tail), successfully changed the list to a stack

Law III: Del (l[0])
>>> del(l[0])

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

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.