删除列表元素的常用代码-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. 4周前
    Xyhtga

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

  10. 4周前
    Saevwe

    doxycycline capsule price - doxycycline hyc 100 mg generic doxycycline online

  11. 4周前
    Ksxunw

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

  12. 4周前
    undonna
  13. 4周前
    Stromectol

    Buy Viagra Chewable Cheap

  14. 4周前
    Afopxp

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

  15. 3周前
    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. 3周前
  17. 3周前
    Bgojgm

    online pharmacy store - tadalafil in india buy cheap cialis online

  18. 3周前
    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. 3周前
    cliette

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

  20. 3周前
    bob viagra

    Cialis Tutta La Vita

  21. 3周前
    Sibrfw

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

  22. 3周前
    sistono

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

  23. 3周前
    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. 3周前
    emurlep
  25. 3周前
    Weerou

    order stromectol online - ivermectin 9mg stromectol cost

  26. 3周前
    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