89DEVs

Python list() function

overview of list()

The list() function constructs and returns a new list. If no argument is passed an empty list is created. If an argument is passed the list contains the elements passed in the iterable.

empty list using list()

If the list() function is called and no argument is passed to it, an empty list is constructed and returned. # empty list using list() myList = list() print(myList) The empty list is constructed and returned. []

list from iterables

If an iterable like a string, list or tuple is passed to the list() function the new list will be filled with the items of the iterable. # list filled with items myString = 'python' myList = list(myString) print(myList) The string is passed as argument to the list() function and the new list contains all letters of the string. ['p', 'y', 't', 'h', 'o', 'n']

syntax of list()

The syntax of the list() function is: list([iterable])

arguments of list()

The list() function accepts a single argument, which is optional. It is an iterable that is used to fill the list.

return value of list()

The list() function constructs and returns a new list. If no arguments are passed to the list() function an empty list is returned. If an iterable is passed as argument to the list() function an list containing the elements of the iterable is returned.

                
        

Summary


Click to jump to section