Python Tutorial Python Advanced Python References Python Libraries

Python - list() Function



The Python list() function (or list() constructor) is used to create list using an iterable object. An iterable object can be any data structure like list, tuple, set, string, dictionary and range iterables etc.

Syntax

list(iterable)

Parameters

iterable Required. iterable like list, tuple, set, string , dictionary, etc.

Return Value

Returns a list containing all elements of the specified iterable.

Example:

In the example below, list() function is used to create list using a given iterable.

#using list iterable
MyList = list(['JAN', 'FEB', 'MAR', 'APR'])
print(MyList)

#using tuple iterable
MyList = list(('JAN', 'FEB', 'MAR', 'APR'))
print(MyList)

#using set iterable
MyList = list({'JAN', 'FEB', 'MAR', 'APR'})
print(MyList)

#using string iterable
MyList = list('string')
print(MyList)

The output of the above code will be:

['JAN', 'FEB', 'MAR', 'APR']
['JAN', 'FEB', 'MAR', 'APR']
['JAN', 'FEB', 'MAR', 'APR']
['s', 't', 'r', 'i', 'n', 'g']

Example: using dictionary iterable

In the example below, list() function is used to create list from a given dictionary.

MyDict = {
  'name': 'John',
  'age': 25,
  'city': 'London'
}
MyList = list(MyDict)
print(MyList)

MyList = list(MyDict.keys())
print(MyList)

MyList = list(MyDict.values())
print(MyList)

The output of the above code will be:

['name', 'age', 'city']
['name', 'age', 'city']
['London', 25, 'John']

❮ Python List Methods