Python List

Python list is ordered data type i.e. with heterogeneous data. List are enclosed in brackets [] with items comma separated. The list is the most widely used data type in Python.

How to create a list in Python?

List in Python is created using big brackets [] with the items in the list be separated by a comma (,).  A list can have any number of items and can be of different data types. To have an idea of python data types, please refer to this tutorial:- Python Datatypes.

Let’s explore some of the examples of the list in the following example:-

The output of the above program is:-

[‘red’, ‘green’, ‘blue’]
[1, 2, 3, 4, 5, 6]
[‘John’, 103, 10.2]
[[1, 2, 3], [4, 5, 6]]

How to access elements in a list in Python?

We can use a slice operator [] and [:] to get items in the list. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator. We can use two types of indexing to get items viz. positive indexing and negative indexing. Positive indexing allows to index items from 0 to n where negative index gives access from backward, where -1 is the last item.

Below is an example of list with simple operations:-


[‘abcd’, 786, 2.23, ‘john’, 70.2]
[786, 2.23]
[2.23, ‘john’, 70.2]

How to change or add elements to a list in Python?

Since lists are mutable data types, you can add, edit or delete every item in a list. It is the most flexible data type in Python. You can use a particular index or slice operator to select more than one items to update the value of the list in that position.

It gives the following output:-
Current list [‘red’, ‘green’, ‘blue’]
Updated new color [‘red’, ‘black’, ‘blue’]

To add elements in the list, append() method of the list is used.

Current list [‘red’, ‘green’, ‘blue’]
Updated new color [‘red’, ‘green’, ‘blue’, ‘gray’]

 How to delete or remove elements from a list in Python?

We can use del function to remove the whole list or index of items in the list.

Current list [‘red’, ‘green’, ‘blue’, ‘gray’, ‘black’]
Updated list [‘red’, ‘blue’, ‘gray’, ‘black’]
Updated list [‘red’, ‘black’]

How to iterate over a List in Python?

A list can be iterated using for loop. Let us see the following example:-


What are Python List Operations?

+Concatenation of two lists
*Repetition operator, to repeat list in multiple times
inMembership operator to check if an item exists in a list
not inMembership operator to check if an item does not exist in a list

What are Python List Built-in functions?

Python provides the following built-in functions which can be used with the lists.

cmp(list1, list2)Compares the elements of left and right lists
len(list)Calculate the length of the list
max(list)Returns the maximum element of the list
min(list)Returns the minimum element of the list
list(seq)Converts any sequence to the list
sum()Returns the sum of all elements in the list
Returns the new sorted list
enumerate()Returns an enumerated object
all()Returns true if all elements in the list are True
any()Returns true if any element in list is True

What are Python List built-in methods?

Python provides various methods that can help you perform the various operations in a list. Following are some of the built-in list methods:-

append()Add an element to the end of the list
count()Returns the count of the number of items passed as an argument
extend() Add all elements of a list to the another list
index() Returns the index of the first matched item
insert() Insert an item at the defined index
pop() Removes and returns an element at the given index
copy()Returns a shallow copy of the list
remove()Removes an item from the list
reverse()Reverse the order of items in the list
sort()Sort items in a list in ascending order
clear()Removes all items from the list


SHARE Python List

You may also like...

Leave a Reply

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