當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python list()用法及代碼示例

python list() 在 python 中創建一個列表。

簽名

list(iterable)

參數

iterable(可選)- 一個可以是序列(字符串、元組等)或集合(集合、字典等)或迭代器對象的對象。

返回

它返回一個列表。

Python list() 函數示例 1

下麵的示例從序列:字符串、元組和列表創建一個列表。

# empty list
print(list())

# string
String = 'abcde'     
print(list(String))

# tuple
Tuple = (1,2,3,4,5)
print(list(Tuple))
# list
List = [1,2,3,4,5]
print(list(List))

輸出:

[]
['a', 'b', 'c', 'd', 'e']
[1,2,3,4,5]
[1,2,3,4,5]

說明:上麵的例子從序列:字符串、元組和列表中創建了一個列表。

Python list() 函數示例2

下麵的示例從 collection:set 和字典創建列表。

# set
Set = {'a', 'b', 'c', 'd', 'e'}
print(list(Set))

# dictionary
Dictionary = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
print(list(Dictionary))

輸出:

['e', 'c', 'd', 'b', 'a']
['c', 'e', 'd', 'b', 'a']




相關用法


注:本文由純淨天空篩選整理自 Python list() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。