追加單個元素:
二、追加元組、列表等:extend() 方法>>> list = ['crazyit', 20, -2]
>>> print(list)
['crazyit', 20, -2, 'fkit']
追加元組中的所有元素:
三、列表中間增加元素:insert() 方法>>> b_list = ['a', 30]
>>> b_list.extend((-2, 3.1))
>>> print(b_list)
['a', 30, -2, 3.1]
>>> a_list = (3.4, 5.6)
>>> b_list.extend(a_list)
>>> print(b_list)
['a', 30, -2, 3.1,3.4, 5.6]
參考:>>> c_list = [1, 2, 3, 4, 5]
>>> c_list.insert(3, 'crazy' )
>>> print(c_list)
[1, 2, 3, 'crazy', 4, 5]
>>> c_list.insert(3, tuple('crazy'))
>>> print(c_list)
[1, 2, 3, ('c', 'r', 'a', 'z', 'y'), 'crazy', 4, 5] # 在索引3處插入元組,元組被當成乙個元素
Python之列表list新增元素
person dev group 徐強 倍總 航神 說明 index值可以為正數 也可以為負數,甚至可以超出list的下標範圍,超出範圍情況將在list的尾部新增乙個元素 person dev group.insert 1,大牛 說明 extend 接收的引數必須為可迭代物件,所有元素會新增到lis...
python 向列表裡面新增元素
python裡面的列表與其他語言的列表不同,雖然python是基於c語言開發的,但是它列表裡面的元素種類可以是混合的,它就是打了激素的陣列。1.列表的元素可以是相同的種類,也可以是不同種類的,如 name 小明 小紅 小華 小海 number 1 2,3 4,5 mix 小明 4 7.88 xiao...
python建立列表和向列表新增元素方法
一.建立列表 1.建立乙個普通列表 tabulation1 大聖 天蓬 捲簾 tabulation1 大聖 天蓬 捲簾 tabulation2 72,36,18 tabulation2 72,36,18 2.建立乙個混合列表 mix tabulation 大聖 72,天蓬 36 syntaxerro...