Python list()
Python list() builtin function is used to create a Python List. list() function optionally takes an iterable as argument and creates a Python List from the elements of the iterable. If no iterable is given, then list() returns an empty list.
In this tutorial, we will learn about the syntax of Python list() function, and learn how to use this function with the help of examples.
Syntax
The syntax of list() function is
list([iterable])
where iterable
is optional.
Returns
The function returns a Python List.
Examples
In the following program, we will create a List of some numbers and strings using list() function.
Python Program
myList = list([25, 1.02, 'abc'])
print(myList)
Output
[25, 1.02, 'abc']
Let us now provide no argument to the list() function. The list() function should return an empty list.
Python Program
myList = list()
print(myList)
Output
[]
Tuple is an iterable. In the following program, we will create a list from a tuple using list().
Python Program
myTuple = (25, 1, 806)
myList = list(myTuple)
print(myList)
Output
[25, 1, 806]
Conclusion
In this Python Tutorial, we have learnt the syntax of Python list() builtin function, and also learned how to use this function, with the help of Python example programs.