motorcycles = ["honda", "yamaha", "suzuki"]
print(motorcycles)
['honda', 'yamaha', 'suzuki']
#使用append方法在列表末尾添加元素,只有一个参数(要添加的元素值),只能添加在列表末尾,其他元素位置不动
motorcycles.append("GEELY")
print(motorcycles)
['honda', 'yamaha', 'suzuki', 'GEELY']
#使用insert方法在列表任意位置添加元素,两个参数(第一个参数为索引位置,第二个为元素),可添加在列表任意位置,该位置及其右侧所有元素都要在原有位置右移一个位置
motorcycles.insert(0, "toyoto")
print(motorcycles)
['toyoto', 'honda', 'yamaha', 'suzuki', 'GEELY']
列表中添加元素的方法append和insert区别
最新推荐文章于 2023-10-11 23:00:00 发布