Python list 串列用法與範例

本篇 ShengYu 要介紹 python list 串列用法與範例,list 串列是個簡單好用的東西,python 常常使用到,一定要學起來,list 串列它可以動態地新增與刪除資料,以下為 list 串列的基本範例。

以下 Python list 內容分為這幾部份,

  • Python 初始化 list 串列
  • 建立空 list 串列
  • 計算 list 串列長度
  • 讀取串列的元素,串列索引
  • 串列索引值為 -1 或 -n
  • 串列切片
  • 添加元素到 list 串列
  • 修改串列元素的內容
  • for 迴圈遍歷巡訪串列裡的元素
  • 刪除 list 串列裡的元素
  • 清空 list 串列裡所有的元素
  • 刪除整個 list 串列

Python 初始化 list 串列

python list 初始化的寫法如下,你可以初始化一個串列裡面都存放整數,也可以都放字串,也可以混合整數跟字串一起放,還可以放其他串列,用 type() 可以檢查這個變數的類型,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
list1 = [1, 2, 3]
print(list1)
print(type(list1))

list2 = ['apple', 'banana', 'orange', 'tomato']
print(list2)
print(type(list2))

list3 = ['hello', 'world', 123, 456]
print(list3)
print(type(list3))

list4 = [list1, list2, list3]
print(list4)
print(type(list4))

輸出如下,

1
2
3
4
5
6
7
8
[1, 2, 3]
<class 'list'>
['apple', 'banana', 'orange', 'tomato']
<class 'list'>
['hello', 'world', 123, 456]
<class 'list'>
[[1, 2, 3], ['apple', 'banana', 'orange', 'tomato'], ['hello', 'world', 123, 456]]
<class 'list'>

建立空 list 串列

如果想建立一個空串列 list 寫法為下,這時 list 裡面是沒有任何資料的,

1
2
3
4
5
6
7
list1 = []
# 或者這樣寫
list2 = list()
print(list1)
print(list2)
print(type(list1))
print(type(list2))

輸出如下,

1
2
3
4
[]
[]
<class 'list'>
<class 'list'>

計算 list 串列長度

python 要計算 list 串列的長度的話可以使用 len() 來計算,

1
2
list1 = [1, 2, 3, 4, 5]
print(len(list1))

輸出如下,

1
5

讀取串列的元素,串列索引

利用串列索引值 index 來取得元素,索引值從 0 開始,第一個元素的索引值為 0,第二個元素的索引值為 1,依此類推,

1
2
3
4
list1 = [1, 2, 3 ,4 ,5]
print(list1[0])
print(list1[1])
print(list1[2])

輸出如下,

1
2
3
1
2
3

串列索引值為 -1 或 -n

串列索引值是 -1 表示串列的最後一個元素,索引值是 -2 表示串列的倒數第二個元素,索引值是 -n 表示串列的倒數第 n 個元素,依此類推,

1
2
3
4
list1 = [1, 2, 3 ,4 ,5]
print(list1[-1])
print(list1[-2])
print(list1[-3])

輸出如下,

1
2
3
5
4
3

串列切片

使用串列切片 slicing 來取得指定範圍內元素,更多串列切片 slicing 的教學範例請看這篇

1
2
3
4
5
6
list1 = [1, 2, 3 ,4 ,5]
print(list1[0:3]) # 串列索引值0到索引值3
print(list1[2:7]) # 串列索引值2到索引值7
print(list1[2:]) # 串列索引值2到最後
print(list1[:4]) # 串列前4個元素
print(list1[:]) # 串列所有元素

輸出如下,

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

添加元素到 list 串列

python 要將元素添加到 list 串列裡的話要使用 append()

1
2
3
4
5
6
7
8
9
10
list1 = [1, 2, 3]
print(list1)
list1.append(4)
list1.append(5)
print(list1)

list2 = []
list2.append(1)
list2.append(2)
print(list2)

輸出如下,

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

修改串列元素的內容

要修改串列元素的內容可以利用索引值來修改,方式如下,

1
2
3
4
5
6
7
8
9
10
list1 = [1, 2, 3 ,4 ,5]
print(list1)
list1[2] = 6
list1[4] = 7
print(list1)

list2 = ['apple', 'banana', 'orange']
print(list2)
list2[0] = 'tomato'
print(list2)

輸出如下,

1
2
3
4
[1, 2, 3, 4, 5]
[1, 2, 6, 4, 7]
['apple', 'banana', 'orange']
['tomato', 'banana', 'orange']

for 迴圈遍歷巡訪串列裡的元素

用 for 迴圈來遍歷巡訪串列 tuple 裡的所有元素並印出來,

1
2
3
list1 = ['hello', 'world', 123, 456]
for i in list1:
print(i)

輸出如下,

1
2
3
4
hello
world
123
456

更多 for 迴圈用法請看這篇

刪除 list 串列裡的元素

python 要刪除 list 串列裡的指定元素的話要使用 del,

1
2
3
4
5
6
list1 = [1, 2, 3, 4, 5]
print(list1)
del list1[0]
print(list1)
del list1[1]
print(list1)

輸出如下,

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

清空 list 串列裡所有的元素

要清空 list 串列裡所有的元素可以使用 clear(),清空後這個 list 裡面就空空了,也就變成一個空 list 串列了,

1
2
3
4
list1 = ['apple', 'banana', 'orange', 'tomato']
print(list1)
list1.clear()
print(list1)

輸出如下,

1
2
['apple', 'banana', 'orange', 'tomato']
[]

刪除整個 list 串列

要刪除整個 list 串列的寫法如下,

1
2
3
4
list1 = [1, 2, 3, 4, 5]
print(list1)
del list1
# print(list1) # error

輸出如下,

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

下一篇將介紹 set 集合的用法

以上就是 Python list 串列用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
如果你想學習 Python 相關技術,可以參考看看下面的文章,
Python 串列切片
Python 檢查 list 列表是否為空
Python 新手入門教學懶人包
Python str 字串用法與範例
Python set 集合用法與範例
Python dict 字典用法與範例
Python tuple 元組用法與範例