删除列表元素的常用代码-del,pop,remove

删除列表内容的三个代码分别为[del],[pop],[remove]。虽然都是删除,但是各有各的删除方式,惯例总结一下,顺带巩固一下知识要点:

#列表删除 【del】 【pop】 【remove】
moviename = ["指环王","盗梦空间","太平洋","夏洛特","指环王"]
print("------删除前的电影-----")
for movielist in moviename:
    print(movielist)

#del moviename[2]  #在指定下标删除一个元素
#moviename.pop()    #弹出末尾最后一个元素
moviename.remove("指环王")  #直接删除指定内容的元素,当列表有重复元素时,remove默认删除找到的第一个元素,而不是删除所有相同内容。

print("-----删除后的电影-----")
for movielist in moviename:
    print(movielist)

如代码所示:

  • del+(空格)+列表名字+[指定的下标] 指定一个元素删除它
  • 列表名字.pop 直接删除列表最后一个元素
  • 列表名字.remove("指定元素") 直接删除指定元素,当列表有重复元素时,默认删除第一个,而不是删除所有的相同元素

再补充一点,当需要修改列表内容的时候,直接指定下标替换为新内容,示例代码:

列表名称[1] = ["新内容"]

括号里的数字为元素位置,后面括号里是要替换的新内容。

26 COMMENTS
  1. 2021/08/03
    Orsdiy

    generic cialis soft tabs - cialis order online pharmacy cialis

  2. 2021/08/04
    Eakrby

    cheap generic viagra in canada - viagra 50mg pill how to buy viagra safely online

  3. 2021/08/12
    Dzglfy

    prednisone 20mg online without prescription - prednisone 5mg without prescription canadian pharmacy prednisone

  4. 2021/08/14
    Xbqjqu

    viagra buy canada - sildenafil online canada purchasing viagra

  5. 2021/08/15
    Xdgqmy

    cialis 15 mg - Cialis online ordering opo to iku 5mg online pharmacy

  6. 2021/08/17
    Ilhxhe

    ivermectin 1 cream 45gm - ivermectin medicine ivermectina 6 mg

  7. 2021/08/19
    Yuoicp

    blue pill for ed - canadian drug pharmacy erectile dysfunction meaning

  8. 2021/08/22
    Qomdqw

    ventolin online - ventolin cost albuterol inhaler

  9. 2021/08/24
    Xyhtga

    misoprostol cytotec buy online - cytotec 600 mcg how to get cytotec in usa

  10. 2021/08/25
    Saevwe

    doxycycline capsule price - doxycycline hyc 100 mg generic doxycycline online

  11. 2021/08/27
    Ksxunw

    neurontin prescription medication - can i buy neurontin over the counter synthroid 75 mcg price

  12. 2021/08/27
    undonna
  13. 2021/08/28
    Stromectol

    Buy Viagra Chewable Cheap

  14. 2021/08/28
    Afopxp

    buy viagra online cheap no prescription - viagra 100 mg price in india

  15. 2021/08/29
    where web hosting

    Thank you for sharing your thoughts. I truly appreciate your efforts and I will be waiting
    for your further post thank you once again.

  16. 2021/08/29
  17. 2021/08/30
    Bgojgm

    online pharmacy store - tadalafil in india buy cheap cialis online

  18. 2021/08/30
    bit.ly

    Hi! I've been following your site for some time now and finally got the
    bravery to go ahead and give you a shout out from
    Atascocita Texas! Just wanted to say keep up the excellent job!

  19. 2021/08/30
    cliette

    https://buytadalafshop.com/ - buy real cialis online

  20. 2021/08/31
    bob viagra

    Cialis Tutta La Vita

  21. 2021/08/31
    Sibrfw

    adc vardenafil generic - generic cialis viagra vardenafil online vardenafil for sale online

  22. 2021/08/31
    sistono

    http://buystromectolon.com/ - where can i buy stromectol

  23. 2021/08/31
    j.mp

    Thanks for your personal marvelous posting! I definitely enjoyed reading it, you might be a
    great author.I will make sure to bookmark your
    blog and will eventually come back sometime soon. I want to encourage you
    to ultimately continue your great job, have a nice weekend!

  24. 2021/09/01
    emurlep
  25. 2021/09/01
    Weerou

    order stromectol online - ivermectin 9mg stromectol cost

  26. 2021/09/01
    is asmr

    It is actually a nice and useful piece of information. I am happy that you simply shared
    this helpful information with us. Please stay us informed like this.
    Thank you for sharing.

loading