Skip to content

Python list() function | Basics

  • by

Python list() is a constructor, which returns a list. If no parameters are passed in the constructor, it returns an empty list. With iterable, it creates a list consisting of iterable’s items.


It can be used in different ways to convert an iterable or other data type into a list.

Here are some common usages of the list() function:

Converting an iterable to a list:

iterable = [1, 2, 3, 4, 5]
new_list = list(iterable)
print(new_list)  # [1, 2, 3, 4, 5]

Creating a list from a string:

text = "Hello, World!"
char_list = list(text)
print(char_list)  # ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']

Creating an empty list:

empty_list = list()
print(empty_list)  # []

Converting a tuple to a list:

my_tuple = (1, 2, 3)
converted_list = list(my_tuple)
print(converted_list)  # [1, 2, 3]

Copying an existing list:

original_list = [1, 2, 3]
copied_list = list(original_list)
print(copied_list)  # [1, 2, 3]

The list() function is quite versatile and can be used to convert various data types to lists. It’s worth noting that if you pass a single argument to the list() function, it must be an iterable (e.g., string, tuple, set, dictionary keys) that can be converted to a list.

Python list() Example

Simple example code.

If you want to create a literal new list with a bunch of new values then you’re right. There is no reason to use the list constructor, you should use the literal notation:

my_list = ['a', 'b', 'c']

Use it to transform iterables into their list representation:

my_tuple = ('a', 'b', 'c')  # create a new tuple
my_list = list(my_tuple) # Convert into a list



Python list() function

You can use the other iterable constructors like set and dict in a similar way.

Python List/Array functions

List of built-in methods that you can use on lists/arrays.

append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the first item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

Do comment if you have any doubts or suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *