Python list() Function

Creates a list from an iterable

Usage

The list() function creates a list from an iterable.

The iterable may be a sequence (such as a string, tuple or range) or a collection (such as a dictionary, set or frozen set)

There is another way you can create lists based on existing lists. It is called List comprehension.

Syntax

list(iterable)

Python list() function parameters
ParameterConditionDescription
iterableRequiredA sequence or a collection

Examples

list() with no arguments creates an empty list.

Example: Create an empty list

L = list()
print(L)    # []

You can convert any sequence (such as a string, tuple or range) into a list using a list() method.

Example: Convert string into list

T = list('abc')
print(T)    # ['a', 'b', 'c']

Example: Convert tuple into list

L = list((1, 2, 3))
print(L)    # [1, 2, 3]

Example: List of successive integers

L = list(range(0, 4))
print(L)    # [0, 1, 2, 3]

You can even convert any collection (such as a dictionary, set or frozen set) into a list.

Example: Convert dictionary keys into list

L = list({'name': 'Bob', 'age': 25})
print(L)    # ['age', 'name']

Example: Convert set into list

L = list({1, 2, 3})
print(L)    # [1, 2, 3]