list()
Builds a new list from anything you can loop over: the characters of a string, the numbers of a range, the keys of a dictionary, or another list.
list(iterable=())list() collects the items of any iterable (anything you can loop over) into a new list. list("abc") gives ['a', 'b', 'c'], list(range(3)) gives [0, 1, 2], and list() with nothing gives an empty list.
You use it to see the results of lazy objects such as range(), map(), zip() and reversed(), to turn a set or a dictionary's keys into something you can index and sort, and to make a copy of a list.
list is also the name of the list type, which is why [1, 2] and list((1, 2)) give the same kind of object. One trap: the copy that list() makes is shallow. If the list holds other lists, the copy shares them.
Parameters
| Parameter | What it is |
|---|---|
iterableoptional, default () | Anything you can loop over: a string, tuple, set, dictionary, range, file, generator and so on. With nothing, you get []. |
Return value
A new list holding the items in the order the iterable gave them.
Examples
1Make lists from other things
print(list("hello"))
print(list(range(1, 6)))
print(list((10, 20, 30)))
print(list())['h', 'e', 'l', 'l', 'o'] [1, 2, 3, 4, 5] [10, 20, 30] []
2See what map(), zip() and reversed() produce
These functions give back lazy objects that print as something like <map object at 0x...>. Wrap them in list() to see the items.
prices = ["4.5", "2", "7.25"]
print(list(map(float, prices)))
print(list(zip(["a", "b"], [1, 2])))
print(list(reversed([1, 2, 3])))[4.5, 2.0, 7.25]
[('a', 1), ('b', 2)]
[3, 2, 1]3The keys of a dictionary
stock = {"pens": 40, "paper": 12, "ink": 3}
names = list(stock)
print(names)
print(names[0])
print(list(stock.values()))['pens', 'paper', 'ink'] pens [40, 12, 3]
Looping over a dictionary gives its keys, in the order they were added.
4Copy a list before changing it
original = ["Mon", "Tue", "Wed"]
copy = list(original)
copy.append("Thu")
print(original)
print(copy)['Mon', 'Tue', 'Wed'] ['Mon', 'Tue', 'Wed', 'Thu']
5Unique tags, sorted
tags = {"python", "web", "data"}
print(sorted(tags))
tag_list = list(tags)
tag_list.sort()
print(tag_list)['data', 'python', 'web'] ['data', 'python', 'web']
A set has no fixed order, so list(tags) alone may come out in any order. Sort it when the order matters.
Common mistakes
Thinking b = a makes a copy
b = a gives the same list a second name: changing b changes a. Use list(a) or a.copy() for a real copy.
a = [1, 2]
b = a
b.append(3)
print(a)
c = list(a)
c.append(4)
print(a, c)[1, 2, 3] [1, 2, 3] [1, 2, 3, 4]
Using list() to split a sentence
list() cuts a string into single characters. To get words, use str.split().
text = "red green"
print(list(text))
print(text.split())['r', 'e', 'd', ' ', 'g', 'r', 'e', 'e', 'n'] ['red', 'green']
Expecting a deep copy
list() copies the outer list only. Inner lists are shared. For nested data use copy.deepcopy().
import copy
grid = [[0, 0], [0, 0]]
shallow = list(grid)
shallow[0][0] = 9
print(grid)
deep = copy.deepcopy(grid)
deep[1][1] = 5
print(grid)[[9, 0], [0, 0]] [[9, 0], [0, 0]]
Questions people ask
What is the difference between list() and []?
For an empty list, none: both give [], and [] is a little faster. Use list(x) when you want to convert something else into a list.
How do I convert a string to a list of words?
Use text.split(). list(text) gives single characters.
How do I convert a list back to a string?
Use str.join(): " ".join(words). Every item must be a string.