Contents
Python – list()
Python list() builtin function returns a list object formed with the items of the given iterable.
If no iterable is passed as argument to list() function, then it returns an empty list object.
Syntax
The syntax of list()
function is
list([iterable])
where iterable
is any iterable like set, list, tuple, etc.
Examples
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)
Run Output
['apple', 'bananna']
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)
Run Output
['a', 'p', 'p', 'l', 'e']
Create an empty list
If no argument is passed to list() function, then it returns an empty list.
Python Program
myList = list()
print(myList)
Run 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.