Python list() function

Python list() is used to create a list from iterable.It is one of way to create list from iterable.


Syntax

iterable(optional): Iterable is optional parameter which can be iterable(tuple,set,string etc) or iterable object.
If iterable is not passed, list() will create empty list.


Return

It returns the list .


Example

Let’s see a very simple example:

l=list() print("Emply list:",l) l1=list((10,20,30,40)) print("List created from tuple:",l1) l2=list({'x':1,'y':2}) print("List created from dictionary:",l2) l3=list(range(5)) print("list created from range function:",l3) l4=list('code2master') print("list created from string:",l4)

Output:

Emply list: []
List created from tuple: [10, 20, 30, 40]
List created from dictionary: [‘x’, ‘y’]
list created from range function: [0, 1, 2, 3, 4]
list created from string: [‘c’, ‘o’, ‘d’, ‘e’, ‘2’, ‘m’, ‘a’, ‘s’, ‘t’, ‘e’, ‘r’]

That’s all about Python list() function.

Leave a Reply

Your email address will not be published. Required fields are marked *