Skip to content

Python list() Function

Last updated on August 8, 2022

In this tutorial, you will learn about the Python list() function with the help of examples.

The list() function creates a list object. And a list object is a collection that is ordered and changeable.

You can use the list() function to convert other sequences or iterator objects to a list.

The list() function creates an empty list if no argument is given to the function.

The syntax of the list() function looks like this:

list([iterable])

list() argument may be a sequence, collection or an iterator object. The bracket tells us that the argument passed to it is optional.

Examples:

my_tuple = (4,3,5,0,1)      # define a tuple
my_string = 'hello world!'   # define a string
my_dict = {'name':"Eyong","age":30,"gender":"Male"}  # define a dict

print("tuple to list: ", list(my_tuple)) # Output: tuple to list:  [4, 3, 5, 0, 1]
print("string to list: ", list(my_string)) # Output: string to list:  ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
print("dict to list: ", list(my_dict)) # Output: dict to list:  ['name', 'age', 'gender']
print("empty list: ", list()) # Output: empty list:  []

Comments are closed, but trackbacks and pingbacks are open.