Python list()

The list() constructor returns a list in Python.

Example


list() Syntax

The syntax of list() is:

list([iterable])

list() Parameters

The list() constructor takes a single argument:

  • iterable (optional) - an object that could be a sequence (string, tuples) or collection (set, dictionary) or any iterator object

list() Return Value

The list() constructor returns a list.

  • If no parameters are passed, it returns an empty list
  • If iterable is passed as a parameter, it creates a list consisting of iterable's items.

Example 1: Create lists from string, tuple, and list

Output

[]
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']

Example 2: Create lists from set and dictionary

Output

['a', 'o', 'u', 'e', 'i']
['o', 'e', 'a', 'u', 'i']

Note: In the case of dictionaries, the keys of the dictionary will be the items of the list. Also, the order of the elements will be random.


Example 3: Create a list from an iterator object

Output

[1, 2, 4, 8, 16]

Recommended Reading: Python List