ScanSkill

list

This list() function is a sequence constructor which returns a list i.e. it converts an object into a list.

Syntax

list(iterable)

Here,

  • iterable: Optional. Any object which is iterable. Iterable may be either a sequence, a container that supports iteration, or an iterator object

Examples

>>> list('foo')
['f', 'o', 'o']
>>> list((1, 2))
[1, 2]
>>> list({1, 2})
[1, 2]
>>> list({'a': 1, 'b': 2}) # Dictionary into list
['a', 'b']
>>> list([1, 2])
[1, 2]
>>> list()
[]
>>> list(iter([1, 2]))
[1, 2]'

Note: If iterable is already a list, a copy is made and returned, similar to iterable[:]. For instance, list(‘abc’) returns [‘a’, ‘b’, ‘c’] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, returns a new empty list, [].