Python 3 reference · Built-in Functions
Python list() Function
Create a list: an ordered, changeable sequence. Called with an iterable, it copies its items into a new list.
Python 3 Since 3.0
Syntax
list(iterable=())
Parameters
| Parameter | Description |
|---|---|
iterable optional |
Items for the new list. |
Return value
A new list.
Examples
print(list("abc"))
print(list(range(5)))
print(list({"x": 1, "y": 2})) # a dict gives its keys
Output
['a', 'b', 'c'] [0, 1, 2, 3, 4] ['x', 'y']
original = [1, 2, 3]
copy = list(original)
copy.append(4)
print(original, copy)
Output
[1, 2, 3] [1, 2, 3, 4]
squares = map(lambda n: n * n, [1, 2, 3])
print(squares) # a lazy map object
print(list(squares)) # materialise it
Output
<map object at 0x1a353e0> [1, 4, 9]
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
tuple()· Create a tuple: an ordered, immutable sequence. Tuples are hashable when their items are, so they can be dict keys.dict()· Create a dictionary: a mapping of keys to values. Build it from keyword arguments, pairs or another mapping.set()· Create a set: an unordered collection of unique items with fast membership tests and set operations.sorted()· Return a new sorted list from any iterable. Supports a key function and reverse order; the original is left unchanged.len()· Return the number of items in a container: characters in a string, items in a list, keys in a dict, and so on.- Python Lists · tutorial