Name

list()

Examples
# Converting a string to a list gives a list with one element per character
alphabet = "abcdefg"
letter_list = list(alphabet)
print(letter_list) # Prints ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# Converting a dictionary to a list gives a list of the dictionary's keys
lettermap = {'a': 1, 'b': 2, 'c': 3}
print(list(lettermap)) # Prints ['a', 'b', 'c']
                         # (possibly in a different order)
# Call list() with another list as an argument to make a copy of the list
inventory = ['lamp', 'skeleton key', 'train ticket']
inventory_copy = list(inventory) # Copy the list
print(inventory_copy) # Prints ['lamp', 'skeleton key', 'train ticket']

# Use list() to convert Python "iterators" to lists
letters = "abcdefg"
reversed_letters = reversed(letters)
print(reversed_letters) # Prints <reversediterator object at 0x2>---
                          # not what we want!

# convert iterator to a list so we can access arbitrary elements, or
# all elements at once
reversed_letters_list = list(reversed_letters)
print(reversed_letters_list) # Prints ['g', 'f', 'e', 'd', 'c', 'b', 'a']

# list() without arguments returns an empty list
empty = list()
print(empty) # prints []
Description Takes an "iterable" object as a parameter and returns a list of items in the object. (An "iterable" object is an object that can be looped over, such as a list, string, dictionary, set or tuple; more information here.) This function is useful for converting a string to list of individual characters, for making copies of existing lists, and for making empty lists.

This function is also useful for converting Python iterators (returned from, e.g., reversed() or the dictionary object's iteritems()) and converting them to lists.
Syntax
list(x)
Parameters
xiterable to convert to a list

Updated on Mon Sep 21 15:53:25 2020.

If you see any errors or have comments, please let us know.