Python list() Function

This article covers the use of the Python list function. The Python list function takes as input an iterable and returns a object of type list. This function allows for greater flexibility and speed while creating lists objects as opposed to creating them normally.

Syntax

The syntax of the list function is as follows.

list([iterable])

It takes as input a single iterable object. An iterable is any object that is a sequence that can be iterated over. Examples of common iterables are lists, arrays, tuples, dictionaries and sets.

You may choose not to pass a iterable object and instead an empty list will be returned by the list function.


Examples

Below are examples of several different types iterables being converted into lists. Note, that with a dictionary the list function will ignore any values and instead only convert the keys of the dictionaries into the list.

>>> list()
[]

>>> list("Hello")
['H', 'e', 'l', 'l', 'o']

>>> list((5,6,7,2))
[5, 6, 7, 2]

>>> list({1:"a", 2:"b", 3:"c"})
[1, 2, 3]

>>> list({1,2,3,4,5})
[1, 2, 3, 4, 5]

This marks the end of the Python list Function. Any suggestions or contributions for CodersLegacy are more than welcome. Questions can be directed to the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments