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
Syntax
list(x)
Parameters
x
Updated on Wed Jan 18 17:32:21 2017.
Creative Commons License