---
title: list() in Python - Scaler Topics
description: list() is a function in Python that creates a list from an iterable, which we will study in this article by Scaler Topics.
author: Ayush Srivastava
category: Python
amphtml:
---
:::section{.abstract}
## Overview
In Python, `list()` is a built-in function used to create a new list object or convert an iterable object (e.g., tuple, string, set) into a list. The `list()` function takes an optional argument, which is the iterable object to be converted into a list. If no argument is passed, an empty list is created. The `list()` function is commonly used in Python to manipulate and store a collection of values, and provides a variety of methods and operations for working with lists, such as appending, removing, sorting, and indexing elements.
:::
:::section{.main}
## Syntax of the list in Python
The syntax of list() function in Python is as follows:
```python
list(iterable)
```
Here, `iterable` is an optional argument that specifies the iterable object to be converted into a list. If no argument is provided, an **empty list** is created.
:::
:::section{.main}
## Parameters of list() in python
The list() function in Python takes an optional argument, which is the `iterable object` that needs to be converted into a list. If no argument is passed, an empty list is created. The type and description of the parameter are as follows:
**iterable (optional)**: An iterable object such as a `tuple`, `string`, `set`, `dictionary`, or any other sequence that needs to be converted to a list. If this argument is not provided, an empty list is created.
:::
:::section{.main}
## Return Values of list() in python
In Python, the built-in list() function is used to create `a new list object` from an iterable or a sequence of values. The list() function can be called without any arguments to create an empty list, or with an iterable as an argument to create a list with the values from that iterable.
:::
:::section{.main}
## Exceptions of list() in python
In Python, the list() function can raise exceptions in certain cases. Here are the possible exceptions that can be raised:
* **TypeError**: If the argument passed to the list() function is not iterable or is not a sequence, a TypeError is raised. This can happen if you pass a non-iterable object such as a number, a string, or a boolean value.
Example:
```python
my_number = 10
my_list = list(my_number) # Raises TypeError
```
* **ValueError**: If the argument passed to the list() function is a string and it contains non-numeric characters, a ValueError is raised.
Example:
```python
my_string = '123a'
my_list = list(my_string) # Raises ValueError
```
* **MemoryError**: If the list() function is called with a very large iterable, it may raise a MemoryError if the system is unable to allocate enough memory to create the list object.
Example:
```python!
my_range = range(10**9) # creates a range object with 1 billion values
my_list = list(my_range) # Raises MemoryError on systems with limited memory
```
It is always a good practice to handle exceptions in your code using try-except blocks to gracefully handle any errors that may occur.
:::
:::section{.main}
## Example of list() in python
```python
my_string = 'hello'
my_list = list(my_string)
print(my_list)
```
This code creates a string my_string with the value 'hello', and then uses the list() function to convert the string to a list of individual characters. The resulting list is assigned to the variable my_list. Finally, the code prints the contents of my_list to the console.
**Output:**
```plaintext
['h', 'e', 'l', 'l', 'o']
```
As you can see, the list() function takes a sequence (in this case, a string) as an argument, and returns a list of the individual items in that sequence. This is a common and useful way to convert between different types of Python objects.
:::
:::section{.main}
## What is a list() in Python?
In Python, `list()` is a built-in function that is used to create a new list object. The list() function can take an `iterable as an argument`, and it `returns a new list object` that contains the items from the iterable. If no argument is provided, it returns an `empty list`.
```python!
# create a list from a string
my_string = "hello"
my_list = list(my_string)
print(my_list) # Output: ['h', 'e', 'l', 'l', 'o']
# create a list from a tuple
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list) # Output: [1, 2, 3]
# create an empty list
my_list = list()
print(my_list) # Output: []
```
The list() function is commonly used to convert other types of objects into lists. For example, you can use the range() function to create a range of numbers, and then convert it into a list using the list() function:
```python!
my_range = range(5)
my_list = list(my_range)
print(my_list) # Output: [0, 1, 2, 3, 4]
```
Overall, the list() function is a useful and powerful tool in Python programming that can help you create, manipulate, and transform lists in various ways.
:::
:::section{.main}
## Python List Methods
Python includes many types of useful list functions that make working with lists way more straightforward. Here are a few of the most common list methods.
| | Methods | Descriptions |
| :---: | :------------: | :------------------------------------------------------------------------------------------------------------: |
| 1. | **append()** | Adds new element to end of the list |
| 2. | **extend()** | Extends the list by adding all the specified iterables like list, string, tuples at the end of the list. |
| 3. | **insert()** | Inserts an element at the defined index |
| 4. | **remove()** | The first occurrence of the specified item is removed from the list. If the provided object is not found, a ValueError is thrown. |
| 5. | **pop()** | Returns and removes an item from the list from the specified index position. The list.pop() method removes and returns the final item in the list if no index is given. |
| 6. | **clear()** | Removes all elements from the list |
| 7. | **count()** | Returns the count of the number of items passed as an argument |
| 8. | **sort()** | Sorts elements of the list in ascending order |
| 9. | **reverse()** | The index locations of the elements in the list are reversed. The first element will be indexed last, and the second element will be indexed second last, and so on. Hence the whole list will be reversed. |
| 10. | **copy()** | Returns a copy of the list |
| 11. | **index()** | The index location of the first occurrence of the specified item is returned. If no item is discovered, a ValueError is thrown. |
:::
:::section{.main}
## More Examples
1. **Creating a list from a range:**
```python!
my_list = list(range(5))
print(my_list) # Output: [0, 1, 2, 3, 4]
```
This creates a list called my_list that contains the numbers 0 through 4.
1. **Creating a list from a string with separator:**
```python!
my_string = "apple,banana,orange"
my_list = my_string.split(",")
print(my_list) # Output: ['apple', 'banana', 'orange']
```
This creates a list called my_list by splitting the string my_string at each comma.
1. **Creating a list from a dictionary:**
```python!
my_dict = {'apple': 2, 'banana': 3, 'orange': 4}
my_list = list(my_dict.items())
print(my_list) # Output: [('apple', 2), ('banana', 3), ('orange', 4)]
```
This creates a list called my_list by converting the dictionary my_dict into a list of tuples containing key-value pairs.
1. Creating a list of squares of numbers:
```python
my_list = [x**2 for x in range(5)]
print(my_list) # Output: [0, 1, 4, 9, 16]
```
This creates a list called my_list that contains the squares of the numbers 0 through 4, using a list comprehension.
Overall, the list() function is a versatile tool in Python that can be used to create lists in various ways, making it a fundamental building block for many Python programs.
:::
:::section{.summary}
## Conclusion
* list() is a built-in function in Python that creates a new list object.
* The list() function can take an iterable as an argument and return a new list object that contains the items from the iterable.
* If no argument is provided, the list() function returns an empty list.
* The list() function can be used to convert other types of objects into lists, including strings, tuples, dictionaries, and ranges.
* Lists are one of the most commonly used data structures in Python, and they are a versatile and flexible tool that can be used in many different applications.
* Using the list() function effectively can help you create more efficient, effective, and elegant Python code.
:::