关于列表的代码append,extend,insert的简单描述

今天学习了几个简单关于列表代码的用法, 这里来做个总结,appendextendinsert的区别所在。


namelist = ["惜儿","乔巴","凸凸","老一","十月月","荼荼"]

[ ] 括号里面内容为列表,用英文逗号隔开。这就是列表,下面几个代码都和列表有关,一并写出来做区分用。


a = [1,2]
b = [3,4]
a.append(b)
print(b)

运行结果为[1, 2, [3, 4]]

这里的append是在末尾添加一个元素,这里把b[3,4]当做一个元素直接添加到a的末尾,而不是当成数字添加到a里面。


a = [1,2]
b = [3,4]
a.extend(b)
print(a)

运行结果为[1, 2, 3, 4]

这里的extend与append很明显的地方就是extend是把b列表里的元素逐一添加到a当中,而不是把b当做一个整体元素来看待。这就是它们明显的差别所在。


a = [1,2,3,4,5]

a.insert(1,9)
print(a)

运行结果为[1, 9, 2, 3, 4, 5]

insert的作用是指定下标位置,插入元素,insert括号里面第一个数字指的是下标位置,后面则为插入的内容。比如例子里面是insert(1,9),则表示把9这个元素插入到a列表里面的第一个位置。说到第一个位置,为什么插入的9在1后面呢,难道不是在第一个吗?

在python里面是从0开始数的,所以1,2,3,4,5 第一下标是2,所以当把9插入到第一下标的时候,就是插入到2的前面。我经常弄错这个位置关系,所以多描述一下避免多次犯错。由于今天下班有点晚,今天就掌握3个关于列表的代码叭!

22 COMMENTS
  1. 2021/08/03
    Yaahwh

    cialis online india - buy tadalafil 20mg price what's the best online pharmacy

  2. 2021/08/04
    Kqkllj

    sildenafil soft tabs generic - how to buy viagra over the counter uk best viagra pills online

  3. 2021/08/12
    Shlypa

    prednisone tablet 100 mg - prednisone 3 tablets daily buy prednisone 10mg online

  4. 2021/08/13
    Wkcxey

    can you buy sildenafil over the counter - canada viagra no prescription buy real viagra no prescription

  5. 2021/08/15
    Iomqpt

    how much is cialis in mexico - Generic cialis best price cialis canada

  6. 2021/08/17
    Mehtac

    ivermectin lotion for scabies - stromectol uk buy ivermectin 4 mg

  7. 2021/08/18
    Ccnxzu

    erectile dysfunction pump - ed drugs what is the main cause of erectile dysfunction

  8. 2021/08/22
    Awmtez

    ventolin cost canada - can i buy ventolin over the counter in usa generic ventolin

  9. 2021/08/24
    Ifvupz

    buy cytotec pills - cytotec 200 mcg price in india buy cytotec mexico

  10. 2021/08/25
    Mjqhpx

    doxycycline 500mg - doxycyclinesp.com canadian pharmacy doxycycline

  11. 2021/08/27
    Jmlfrc

    neurontin cost in singapore - levoxyl levothyroxine synthroid 25

  12. 2021/08/28
    Wwebfd

    purchase viagra - where can i buy generic viagra online safely

  13. 2021/08/29
    web hosting on

    It's remarkable for me to have a site, which is beneficial for my know-how.

    thanks admin

  14. 2021/08/29
    Cialis

    Discount Tadalifil

  15. 2021/08/30
    Xabihl

    brand cialis online canada - discount tadalafil us generic cialis

  16. 2021/08/30
    j.mp

    Keep on working, great job!

  17. 2021/08/31
    Gakoxy

    buy vardenafil with no prescription - cialis generic vardenafil viagra vardenafil uk

  18. 2021/08/31
    j.mp

    magnificent issues altogether, you just received a
    logo new reader. What could you suggest about your publish that you
    made a few days in the past? Any certain?

  19. 2021/08/31
    loyache
  20. 2021/09/01
    Jdrqud

    ivermectin pills canada - buy stromectol uk ivermectin 18mg

  21. 2021/09/01
    it scoliosis surgery

    Hi there! This post could not be written any better!

    Looking through this post reminds me of my previous roommate!
    He constantly kept talking about this. I most certainly will forward this article to him.
    Pretty sure he'll have a very good read. Many thanks for sharing!

  22. 2021/09/02
    bitly.com

    I think the admin of this web site is really working hard in favor of his
    web page, as here every information is quality based stuff.

loading