Home Python Built-in Functions list() Python list() Function Definition In Python, list(iterable) creates a new list containing every item of the iterable, in order: list("abc") is ['a', 'b', 'c']. list() with no argument returns an empty list, the same as [].
It is the usual way to turn lazy objects such as range , map , or zip into a real list you can index and print. Passing an existing list makes a shallow copy : a new outer list that still shares the inner objects.
list is a built-in type (a class) that you call like a function: each call creates a new list object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax Pass any iterable; you always get a fresh list.
items = list(iterable)
empty = list() Call What it does list("abc")Returns ['a', 'b', 'c']. list(range(3))Returns [0, 1, 2]. list(d)Returns the dict's keys in insertion order. list(old)A shallow copy of another list.
list(x) always creates a new object, even when x is already a list.
Library declarations list(iterable=(), /)
# class list: a mutable built-in type Parameters Parameter Description iterable Optional. Any iterable: string, list, tuple, set, dict, range, generator, file, ... Default: empty.
Return Value Return Value Description list A new list with the iterable's items.
Python Examples 1. From iterables 2. Shallow copy 3. Lazy iterators Example 1 — Build Lists from Different Iterables Convert a string, a range, a tuple, and a dict into lists.
print(list("abc"))
print(list(range(5)))
print(list((1, 2, 3)))
print(list({"x": 1, "y": 2}))
print(list()) ['a', 'b', 'c']
[0, 1, 2, 3, 4]
[1, 2, 3]
['x', 'y']
[] How It Works Strings split into characters. Iterating a string yields one character at a time.Ranges expand. list(range(5)) materializes the numbers 0 to 4.Dicts give keys. Iterating a dict yields its keys, in the order they were inserted.No argument. list() is an empty list.Example 2 — list() Makes a Shallow Copy Copy a list that contains another list and change both levels.
original = [1, 2, [3, 4]]
copy = list(original)
copy.append(5)
copy[2].append(99)
print("original:", original)
print("copy: ", copy)
print(copy is original, copy[2] is original[2]) original: [1, 2, [3, 4, 99]]
copy: [1, 2, [3, 4, 99], 5]
False True How It Works New outer list. append(5) on the copy does not touch original.Shared inner list. copy[2] and original[2] are the same list object, so 99 shows up in both.Identity check. copy is original is False, but the nested items are identical objects.Deep copies. Use copy.deepcopy(original) when nested data must be independent.Example 3 — Turn Iterators and dict.items() into Lists See that an iterator can be consumed only once, and build lists of pairs and rows.
squares = map(lambda n: n * n, [1, 2, 3])
print(list(squares))
print(list(squares))
prices = {"tea": 3, "cake": 5}
pairs = list(prices.items())
print(pairs)
print(pairs[0])
matrix = [list(row) for row in ["ab", "cd"]]
print(matrix) [1, 4, 9]
[]
[('tea', 3), ('cake', 5)]
('tea', 3)
[['a', 'b'], ['c', 'd']] How It Works Lazy until listed. map() computes nothing until list() pulls the values out.One pass only. The second list(squares) is empty because the iterator is already exhausted.Indexable pairs. list(prices.items()) gives tuples you can index, unlike the items view itself.Lists of lists. Calling list() on each string builds a small character grid.Frequently Asked Questions What does list() do in Python? It creates a new list from an iterable, such as list("abc") or list(range(5)). With no argument it returns [].
Is list(x) the same as [x]? No. list("ab") gives ['a', 'b'], while ["ab"] is a one-item list. list(5) fails because an int is not iterable.
Does list() copy a list? Yes, it makes a shallow copy. Nested objects are shared; use copy.deepcopy() for a full copy.
How do I convert a dict to a list? list(d) gives the keys, list(d.values()) the values, and list(d.items()) the key/value pairs.
Why does my second list(iterator) return []? Iterators can be read only once. Store the first list() result, or recreate the iterator.
Next: locals() Get a dictionary of the current local variables.
locals() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion