What are Lists?

The List data type in Python is an ordered sequence of elements of one or more data types, such as integers, characters, float and even a list too! It is a mutable data type, which means its elements can be modified or changed after creation.

Lists are very useful and are essentially used more often than not in any Python program.

What are Python List Methods?

Python List Methods are the built-in methods in lists used to perform operations on Python lists.

List Methods in Python

MethodDescription
list()Creates an empty list if no argument is passed.
Creates a list if a sequence (eg. string) is passed as an argument.
len()Returns the length or number of elements in the list passed as an argument.
append()Appends a single element at the end of the list.
extend()Appends each element of an iterable passed as an argument to the end of given list.
insert()Inserts an element at a particular index in the list.
count()Returns the number of times a given element appears in the list.
index()Returns index of the first occurrence of the element in the list.
If the element is not present, ValueError is raised.
remove()Removes the first occurrence of the given element in the list.
If the element is not present, ValueError is raised.
pop()Returns the element whose index is passed as parameter to this function and also removes it from the list. If no parameter is given, then it returns and removes the last element of the list.
clear()Removes all the elements from the list.
reverse()Reverses the order of elements in the given list.
sort()Sorts the elements of the given list.
sorted()It takes a list as parameter and creates a new list consisting of the same elements arranged in sorted order.
min()Returns the minimum or smallest element in the list.
max()Returns the maximum or largest element in the list.
sum()Returns the sum of all the elements of the list.

Adding elements in List

1. Python append() method

It is used for adding an element to the end of the list. The single element can also be a list.

Syntax:

list.append(element)

Example 1:

Python
list1 = [10,20,30,40]
list1.append(50)
print(list1)

Output:

[10, 20, 30, 40, 50]

Example 2:

Python
list1 = [10,20,30,40]
list1.append([50,60])
print(list1)

Output:

[10, 20, 30, 40, [50, 60]]

2. Python extend() method

It adds all the elements of an iterable (like list, string, tuple) to the end of the given list. The iterable remains unchanged.

Syntax:

list1.extend(list2)

Example:

Python
list1 = [10,20]
list2 = [30,40]
list1.extend(list2)
print(list1)
print(list2)

Output:

[10, 20, 30, 40]
[30, 40]

3. Python insert() method

This method is used to insert an element at a particular index in the list. If the index is out of range, then it inserts the element at the end of list.

Syntax:

list.insert(position, element)

Example:

Python
list1 = [10,20,30,40,50]
list1.insert(2,25)
print(list1)
list1.insert(10,55)    # index given is out of range
print(list1)

Output:

[10, 20, 25, 30, 40, 50]
[10, 20, 25, 30, 40, 50, 55]

Deleting elements in List

1. Python remove() method

This method removes the given element from the list. If the element is present multiple times, only the first occurrence is removed. If the element is not present, then ValueError is generated.

Syntax:

list.remove(element)

Example:

Python
list1 = [10,20,30,40,50,20]
list1.remove(20)
print(list1)

Output:

[10, 30, 40, 50, 20]

2. Python pop() method

This method returns the element whose index is passed as parameter to this function and also removes it from the list. If no parameter is given, then it returns and removes the last element of the list. If index given is out of range, then an IndexError is raised.

Syntax:

list.pop([index])

Example:

Python
list1 = [10,20,30,40,50]
a = list1.pop()    # No index is given, so it will return and delete the last element
print(list1)
b = list1.pop(2)
print(list1)
print("Value of a =", a)
print("Value of b =", b)

Output:

[10, 20, 30, 40]
[10, 20, 40]
Value of a = 50
Value of b = 30

3. Python del method

This method deletes an element from the list using its index. If no index is given, it deletes entire list from the memory.

Syntax:

del list[index]

Example 1:

Python
list1 = ['A','B','C','D','E']
del list1[2]
print(list1)

Output:

['A', 'B', 'D', 'E']

Example 2:

Python
list1 = [1,2,3,4,5]
del list1
print(list1)    # It will throw an error since list1 is completely deleted from the memory

Output:

Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    print(list1)
NameError: name 'list1' is not defined. Did you mean: 'list'?

4. Python clear() method

This method removes all the elements from the list.

Syntax:

list.clear()

Example:

Python
list1 = [1.1,2.2,3.3,4.4,5.5]
list1.clear()
print(list1)

Output:

[]

NOTE: The del method completely deletes the list from the memory and the list can no longer be accessed through its variable name. On the other hand, clear() method just removes all the elements of the list and it can still be accessed through its variable name, even though its empty.

Some important Python List Methods

1. Python list() method

This method creates a list from the iterable (like string, tuple, list) passed as an argument. If no argument is given, it creates an empty list.

It is a very useful method, since it can be directly used to create a list from any iterable like string. For example, we can use it to easily create a list of characters of a string, as illustrated below. Moreover, it can also be used to copy one list to another.

Syntax:

List = list([iterable])

Example:

Python
list1 = list("LearnersEpoint")
print(list1)
list2 = list()    # Creates an empty list
print("Empty list2:\n", list2)

# When below line is executed, all elements of list1 are copied to list2. This can also be done directly without creating an empty list.
list2 = list(list1)
print("list2 after copying list1 elements to it:\n", list2)

Output:

['L', 'e', 'a', 'r', 'n', 'e', 'r', 's', 'E', 'p', 'o', 'i', 'n', 't']
Empty list2:
[]
list2 after copying list1 elements to it:
['L', 'e', 'a', 'r', 'n', 'e', 'r', 's', 'E', 'p', 'o', 'i', 'n', 't']

PS: This saved us some loops 🙂

2. Python len() method

len() method in Python can be used to calculate the length or number of elements of not only lists, but any iterable like string, tuple, etc.

Syntax:

len(list_name)

Example:

Python
list1 = [1,2,3,4,5]
length = len(list1)
print("Length of list1 is:", length)

Output:

Length of list1 is: 5

3. Python count() method:

This method is used to calculate the frequency of a given element in the list.

Syntax:

list.count(element)

Example:

Python
list1 = [7,0,9,2,0,2,3]
count_of_0 = list1.count(0)
print("Frequency of 0 in list1 is:", count_of_0)

Output:

Frequency of 0 in list1 is: 2

4. Python index() method

This method returns index of the first occurrence of the element in the list. If the element is not present, ValueError is raised. Start (including) and End (excluding) parameters can also specified to search in a given range. However, they are not necessary and if not specified, the method searches complete list.

Syntax:

list.index(element[,start[,end]])

Example:

Python
list1 = [10,20,30,40,50]
index_of_40 = list1.index(40)
print("40 is present at index", index_of_40)

Output:

40 is present at index 3

5. Python reverse() method

This method reverses the order of elements in the given list.

Syntax:

list.reverse()

Example:

Python
list1 = [10,20,30,40,50]
print("List before reversing:", list1)
list1.reverse()
print("List after reversing:", list1)

Output:

List before reversing: [10, 20, 30, 40, 50]
List after reversing: [50, 40, 30, 20, 10]

6. Python sort() method

This method is used to sort the elements of the list (or tuple) in ascending or descending order. By default, it sorts in the ascending order but if Key and reverse_flag are also specified, then it will sort in descending order.

Syntax:

list.sort()

Example 1:

Python
list1 = [10,40,50,20,30]
print("List before sorting:", list1)
list1.sort()
print("List after sorting:", list1)

Output:

List before sorting: [10, 40, 50, 20, 30]
List after sorting: [10, 20, 30, 40, 50]

Example 2:

Python
list1 = [10,40,50,20,30]
print("List before sorting:", list1)
list1.sort(reverse = True)
print("List after sorting:", list1)

Output:

List before sorting: [10, 40, 50, 20, 30]
List after sorting: [50, 40, 30, 20, 10]

7. Python sorted() method

This method takes a list as parameter and creates a new list consisting of the same elements arranged in sorted order. Note that sorted() method does not sort the original list.

Syntax:

sorted(list)

Example:

Python
list1 = [10,40,50,20,30]
list2 = sorted(list1)
print("Sorted list:", list2)
print("Original list:", list1)    # Note that the original list is still unsorted

Output:

Sorted list: [10, 20, 30, 40, 50]
Original list: [10, 40, 50, 20, 30]

8. Python min() method

This method returns the smallest element of the list (or iterable).

Syntax:

min(iterable)

Example:

Python
list1 = [10,20,30,40,50]
minimum = min(list1)
print("Smallest element of the list is:", minimum)

Output:

Smallest element of the list is: 10

9. Python max() method

This method returns the largest element of the list (or iterable).

Syntax:

max(iterable)

Example:

Python
list1 = [10,20,30,40,50]
maximum = max(list1)
print("Largest element of the list is:", maximum)

Output:

Largest element of the list is: 50

10. Python sum() method

This method returns the sum of all the elements of the list (or tuple).

Note: The sum is calculated only for numerical values, if any other data type is present in the list, then sum() method throws TypeError.

Syntax:

sum(list)

Example:

Python
list1 = [10,20,30,40,50]
sum_list1 = sum(list1)
print("Sum of elements of the list is:", sum_list1)

Output:

Sum of elements of the list is: 150

By Piyush Poddar

I am an undergraduate student with keen interest in Linux, Python and AI/ML. I love to work on Open Source technologies and contribute using my skills in Open Source projects. Also, passionate about automation and writing technical blogs.

Leave a Reply

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