🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

list()

AI & DATA SCIENCE // list

list() builds a new list, either empty or from the elements of any iterable such as a string, tuple, dict, or generator.

Syntax

list()
list(iterable)

Deep Dive Course

list() is both the type of Python's built-in mutable, ordered sequence and a constructor for it. Called with no arguments it returns an empty list; called with an iterable, it eagerly consumes every element and collects them, in order, into a new list — the standard way to materialize a lazy iterator, like the result of map() or a generator, into something you can index or iterate over more than once.

1Understanding list()

list() is both the type of Python's built-in mutable, ordered sequence and a constructor for it. Called with no arguments it returns an empty list; called with an iterable, it eagerly consumes every element and collects them, in order, into a new list — the standard way to materialize a lazy iterator, like the result of map() or a generator, into something you can index or iterate over more than once.

💡

list(some_list) creates a shallow copy — a new list containing the same element references — which is a quick, readable way to duplicate a list without mutating the original.

editor.html
letters = list("abc")
print(letters)
numbers = list(range(5))
print(numbers)
localhost:3000

2Practical Example

Here is a real-world application of list() showing how it is used in production Python code.

editor.html
squares_iter = map(lambda x: x ** 2, range(5))
squares = list(squares_iter)
print(squares)
localhost:3000

3Best Practices

Follow these guidelines when working with list():

1. Use list(iterable) to eagerly evaluate a generator or map/filter object when you need to reuse it multiple times

2. Prefer a list literal over calling list() on a tuple for readability when writing literals by hand

3. Use list comprehensions instead of wrapping map() in list() when a transformation also needs filtering or is easier to read inline

⚠️

Tip: list(some_list) creates a shallow copy — a new list containing the same element references — which is a quick, readable way to duplicate a list without mutating the original.

editor.html
letters = list("abc")
print(letters)
numbers = list(range(5))
print(numbers)
localhost:3000

Examples

Example 01Basic Usage
letters = list("abc")
print(letters)
numbers = list(range(5))
print(numbers)
Example 02Advanced Example
squares_iter = map(lambda x: x ** 2, range(5))
squares = list(squares_iter)
print(squares)

Best Practices

  • Use list(iterable) to eagerly evaluate a generator or map/filter object when you need to reuse it multiple times
  • Prefer a list literal over calling list() on a tuple for readability when writing literals by hand
  • Use list comprehensions instead of wrapping map() in list() when a transformation also needs filtering or is easier to read inline

Interview Question

What's the difference between a list literal like [1, 2, 3] and calling list() on an existing tuple?

Hint: Think about what each syntax actually does under the hood.

A list literal builds the list directly from the elements you write inside the brackets. Calling list() on a tuple instead invokes the list constructor, iterating over the tuple and copying its elements into a new list — functionally the result is identical, but list() is a conversion/constructor call while the literal is dedicated syntax, so the literal is slightly faster and more idiomatic when you're writing the values by hand.

Exercises

MediumPractice using list() in a real scenario.
View Solution
letters = list("abc")
print(letters)
numbers = list(range(5))
print(numbers)

Frequently Asked Questions

What's the difference between a list literal like [1, 2, 3] and calling list() on an existing tuple?

A list literal builds the list directly from the elements you write inside the brackets. Calling list() on a tuple instead invokes the list constructor, iterating over the tuple and copying its elements into a new list — functionally the result is identical, but list() is a conversion/constructor call while the literal is dedicated syntax, so the literal is slightly faster and more idiomatic when you're writing the values by hand.

Related Functions

tuple()list comprehensionsappend()