list() Builtin Function
Python - list()
Python list() builtin function is used to create a list from the given iterable. If no iterable is passed as argument to list() function, then it returns an empty list object.
In this tutorial, you will learn the syntax of list() function, and then its usage with the help of example programs.
Syntax
The syntax of list()
function is
list([iterable])
where
Parameter | Description |
---|---|
iterable | [Optional] An iterable like set, list, tuple, etc. |
The function returns a list type object.
Examples
1. Create a list from given set
In the following program, we take a set of strings as iterable, and pass this iterable to list() function, to create a new list from the elements of the set.
Python Program
mySet = {'apple', 'bananna'}
myList = list(mySet)
print(myList)
Output
['apple', 'bananna']
2. Create list of characters from given string
In the following program, we pass a string as an argument to list() function. The function returns a list of characters.
Python Program
myStr = 'apple'
myList = list(myStr)
print(myList)
Output
['a', 'p', 'p', 'l', 'e']
3. Create an empty list
If no argument is passed to list() function, then it returns an empty list.
Python Program
myList = list()
print(myList)
Output
[]
Summary
In this tutorial of Python Examples, we learned the syntax of list() builtin function, and how to use this function to create a list from the items of the given iterable, with examples.