
In Python, the built-in list() function converts an iterable object, such as a string or tuple, to a list.
For example, let’s convert a string to a list of characters:
>>> list("Hello") ['H', 'e', 'l', 'l', 'o']
In this guide, you are going to see examples of using the list() function and how to make a custom object support it.
When Use list() Function in Python
Use the list() function whenever you need to convert something to a list in Python.
For example, when filtering a list, you get back a filter object.
To convert the filter object to a list, use the list() function:
ages = [32, 2, 17, 90, 23] adults = filter(lambda x: x >= 18, ages) adults = list(adults) print(adults)
Output:
[32, 90, 23]
Examples
Let’s see a bunch of examples of converting from iterables to lists in Python.
List from a Tuple
A tuple is an immutable collection of values in Python. This means you cannot modify the contents of a tuple after creation. However, you can convert a tuple to a list that you can modify if you want to.
For instance:
>>> ages = 10, 20, 30 >>> list(ages) [10, 20, 30]
List from a Set
A set is a unique collection of elements in Python. A set does not have an order. You can see this when you convert a set to a list.
For example:
>>> names = {"Alice", "Bob", "Charlie"} >>> list(names) ['Bob', 'Charlie', 'Alice']
List from a String
A string is also an iterable type in Python. In other words, you can convert a string to a list. This returns the characters of the string as a list.
For example:
>>> string = "Hello world" >>> list(string) ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
List from a Dictionary
When you convert a dictionary to a list in Python, the keys are returned.
For example:
>>> data = {"name": "Alice", "age": 40, "address": "Imaginary Road 26"} >>> list(data) ['name', 'age', 'address']
The list() Function without a Parameter
If you give the list() function no parameter at all, a new empty list is created.
For example:
empty = list() print(empty)
Output:
[]
Advanced Example: Call list() on a Custom Object
As you know, you can call list() function on any iterable object in Python.
But how about calling list() on a custom object?
Let’s play with a custom class Fruit. We would like to be able to call the list() function on a Fruit object to get a list of characters in the name of the fruit.
To understand this section, you should have a good understanding of iterators and iterables in Python.
Here is the Fruit class:
class Fruit: def __init__(self, name): self.name = name
Now let’s create a Fruit object and call list() function on it:
banana = Fruit("Banana") letters = list(banana) print(letters)
Output:
TypeError: 'Fruit' object is not iterable
As you can see, the error says it is not possible to convert a Fruit to a list because it is not iterable.
So the only way to make a Fruit convertible to a list is by making it iterable.
But how?
By definition, an iterable is an object that implements the __iter__() method in the class that returns an iterator.
Without digging too deep into the details, let’s implement the __iter__() method in the Fruit class.
As you know, a string is already an iterable object. This means the str type implements the __iter__() method. To get the characters of the Fruit as a list, you can thus directly call iter() method on the name attribute of the Fruit object. This returns an iterator, that you can return from the custom __iter__() method:
class Fruit: def __init__(self, name): self.name = name def __iter__(self): return iter(self.name)
Now you can call list() function on a Fruit object:
banana = Fruit("Banana") letters = list(banana) print(letters)
Output:
['B', 'a', 'n', 'a', 'n', 'a']
A Bonus Example: Remove Duplicates from a List
A common example of using the list() function is when removing duplicates from a list. To do this:
- Convera a list to a dictionary with the dict.fromkeys() function. This removes all duplicate elements as there can be none in a dictionary.
- Convert the dictionary back to a list using the list() function.
For example:
nums = [1, 2, 2, 2, 3, 2, 4, 5] nums = list(dict.fromkeys(nums)) print(nums)
Output:
[1, 2, 3, 4, 5]
Conclusion
Today you learned what is the list() function in Python.
To recap, the list() function is a built-in function that allows converting any iterable object to a list.
Thanks for reading.
Happy coding!