Python list() function | Basics

  • by

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


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 and suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.