In this tutorial we will learn about the python list() method and its uses with examples.
What is the Python list() Method?
The python list() method is used to create a list along with typecasting of the list.
A list is a python built-in data structure that is a collection of ordered and mutable objects. You can learn more about python lists from here(Link).
The syntax of python list() is:
Python list() Parameters
The list() method takes only one parameter as argument:
- iterable (optional) – an iterable object that can be a sequence or collection of objects. (that can include string, tuples, set, or dictionary)
Example 1: How to use the list() method in python?
Example 2: How to convert a dictionary to list using list() method?
When we try to convert a dictionary to a list, only keys will be the elements of the list. It will not take values in the list. Also, the order of the elements will be random.
Example 3: How to create an empty list in python?
Rules of list() method
- If the list() method is passed without a parameter it will return an empty list.
- Dictionary object will only return keys when passed in the list() method.
- Only sequences or collections of objects can be used with the list() method.