Python list() Function

The list() constructor creates a list in Python.

The syntax of list() constructor is:


Recommended Reading: Python list and how to work with them.

list() Parameters

Python list() constructor takes a single argument:

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

Return value from list()

The list() constructor returns a mutable sequence list of elements.

  • If no parameters are passed, it creates an empty list
  • If iterable is passed as parameter, it creates a list of elements in the iterable

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

# empty list

# vowel string
vowelString = 'aeiou'

# vowel tuple
vowelTuple = ('a', 'e', 'i', 'o', 'u')

# vowel list
vowelList = ['a', 'e', 'i', 'o', 'u']

When you run the program, the output will be:

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

Example 2: Create list from collection: set and dictionary

# vowel set
vowelSet = {'a', 'e', 'i', 'o', 'u'}

# vowel dictionary
vowelDictionary = {'a': 1, 'e': 2, 'i': 3, 'o':4, 'u':5}

When you run the program, the output will be:

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

Note: Keys in the dictionary are used as elements of the returned list. Also, the order in the list is not defined as a sequence.

Example 3: Create list from custom iterator object

class PowTwo:
    def __init__(self, max):
        self.max = max
    def __iter__(self):
        self.num = 0
        return self
    def __next__(self):
        if(self.num >= self.max):
            raise StopIteration
        result = 2 ** self.num
        self.num += 1
        return result

powTwo = PowTwo(5)
powTwoIter = iter(powTwo)


When you run the program, the output will be:

[1, 2, 4, 8, 16]