list()
Published Nov 10, 2022Updated May 11, 2023
Contribute to Docs
The list()
function returns a list from an iterable.
Syntax
list(iterable)
The iterable
parameter can be one of the following:
- A sequence type such as a string, range object, list, or
tuple
. - An iterable container such as a list, tuple,
dictionary
, orset
. - An iterable object such as one returned by the
iter()
function.
If no iterable
is passed, the list()
function returns an empty list.
Example
The example below demonstrates how the list()
function is used to create a list from a dictionary:
language_difficulties = list({"Python" : "Hard", "Java" : "Medium", "HTML" : "Easy"})print(language_difficulties)empty_list = list()print(empty_list)
This will print the following output:
['Python', 'Java', 'HTML'][]
Codebyte Example
The following examples shows three different cases of using the list()
function:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours