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


Python list()用法及代碼示例

Python list() 函數

list() 函數是一個庫函數,用於創建一個列表,它接受括號內的多個元素(因為 list() 隻接受一個參數。因此,括號內的元素集合被視為單個參數)。

用法:

    list((elements))

參數: elements– 元素列表。

返回值: list– 它返回給定元素的列表。

例:

    Input:
    students = list(("Amit shukla", "prem", "Radib", "Abhi"))

    Output:
    students: ['Amit shukla', 'prem', 'Radib', 'Abhi']

使用 list() 函數創建列表的 Python 代碼

# python code to demonstrate example of
# list() method 

# creating list
students = list(("Amit shukla", "prem", "Radib", "Abhi"))

# printing type of list() function
print("type of list() function:", type(students))

# printing list...
print("students:", students)

輸出

type of list() function: <class 'list'>
students: ['Amit shukla', 'prem', 'Radib', 'Abhi']


相關用法


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