Lists are undoubtedly one of the most powerful and versatile objects in Python and the list()
function can be used to create a list object.
Python list() Syntax
The syntax of list()
method is as follows:
list([iterable])
list() Parameters
The list()
method takes only a single argument:
- iterable (optional) - This parameter refers to an object that can be a sequence such as a string, tuple, etc. or a collection such as a set, dictionary, etc. or any iterator object.
list() Return Value
The list()
method is basically a constructor and it returns a list. Based on the input parameters, there can be two type of return values from the list()
method:
Return Value | Details |
---|---|
List of iterable items | If an iterable is passed as a parameter, a list containing iterable elements is returned. |
Empty List | If no parameters are passed, an empty list is returned. |
Python list() Examples
# list from string
st = "abc"
ls1 = list(st)
print(ls1)
# list from tuple
tup = ('d','e','f')
ls2 = list(tup)
print(ls2)
# list from dictionary
dic = {'g':1, 'h':2, 'i':3}
ls3 = list(dic)
print(ls3)
# empty dictionary
ls4 = list()
print(ls4)
Output:
['a', 'b', 'c']
['d', 'e', 'f']
['g', 'h', 'i']
[]
In this way we have seen the working of the Python list()
function and how it can be used to convert an iterable or a sequence into a list object.