In this Python tutorial, you will learn how to initialize a list using list() built-in function or square bracket notation.
Python – Initialize List
To initialize a List in Python, we can use list() built-in function; or take the elements, separated by comma, enclosed in square brackets and assigned to a variable.
The syntax to initialize a Python List using list() function is
myList = list([element_1, element_2, element_3, ...])
The syntax to initialize a Python List with elements directly assigned to a variable is
myList = [element_1, element_2, element_3, ...]
Examples (2)
1. Initialize a list using list() function
In this example, we will initialize a list using list() builtin function. We shall print the type of the object returned by list() and print the object itself.
Python Program
myList = list([5, 7, 3, 9])
print(type(myList))
print(myList)
Output
<class 'list'>
[5, 7, 3, 9]
type(myList)
returned <class 'list'>
meaning that the object returned by the list() function is a Python List.
2. Initialize a list directly with elements using square bracket notation
In this example, we will initialize a list with elements enclosed in square brackets to a variable.
Python Program
myList = [5, 7, 3, 9]
print(type(myList))
print(myList)
Output
<class 'list'>
[5, 7, 3, 9]
Conclusion
In this Python Tutorial, we learned how to initialize a Python List with example programs.