Python list methods

Python List Methods to work with

In our previous two chapters, we explored the concepts of Lists in Python. We will continue discussing Python Lists in this post as well. Every programming language has in-built methods and functions to perform a wide range of operations in the program. Python also has its own sets of in-built functions or methods that can do wonders to the code in your Python program. Let us explore in this post what the Python List methods can do to the Lists in your program.


Link to all chapters in Python: Learn Python


Python List Methods to make your code simpler

If you have been following our blog since the beginning, you might have already come across a few functions such as max(), min(), and len() that help in retrieving the highest number, smallest number, and length of string or list. Similarly, we have methods in Python that you can use on Lists in your Python program.

Python List Methods & its use

MethodUse
clear()Removes all elements from the list
reverse()To reverse the order of elements in a list
sort()To sort the elements of a list in ascending order
pop()Removes the last element from the list
remove()Removes the element from the list
count()To know the number of occurrences of an element in a list
extend()Appends one list to another list
copy()Copies all the list elements into a new list
insert()Inserts an element at a particular index position in the list
append()Adds an element at the end of the list
index()To know the first occurrence of any element in a list
sum()To know the sum of all elements in a list

Let us now look at a program using these methods

#Python program to work with in-built methods
#on Python Lists

list_example=[11,22,33,44,55]

print("The declared list is: \n",list_example)
print("The number of elements in the list is: {}".format(len(list_example)))
list_example.append(66)
print("The updated list after adding 66 at the end: \n{}".format(list_example))
list_example.insert(3,77) #inserting 77 at the index position 3
print("The updated list after inserting 77 at index position 3: \n",list_example)
list_example2=list_example.copy() #creates a copy of the list_example
print("The new list_example2 is: \n",list_example2)
list_example.extend(list_example2) 
print("list_example after appending list_example2: \n",list_example)
print("Number of occurrences of 44 in the current list: ",list_example.count(44))
list_example.remove(44)
print("list_example after removing 44: ",list_example)
print("The last element of the list that we've removed using the pop method: ",list_example.pop())
list_example.sort()
print("list_example after sorting: ",list_example)
list_example.reverse()
print("list_example after reversing: ",list_example)
list_example.clear()
print("list_example after clearing all the elements in the list: \n",list_example)
Output

The declared list is:
 [11, 22, 33, 44, 55]
The number of elements in the list is: 5
The updated list after adding 66 at the end:
[11, 22, 33, 44, 55, 66]
The updated list after inserting 77 at index position 3:
 [11, 22, 33, 77, 44, 55, 66]
The new list_example2 is:
 [11, 22, 33, 77, 44, 55, 66]
list_example after appending list_example2:
 [11, 22, 33, 77, 44, 55, 66, 11, 22, 33, 77, 44, 55, 66]
Number of occurrences of 44 in the current list:  2
list_example after removing 44:  [11, 22, 33, 77, 55, 66, 11, 22, 33, 77, 44, 55, 66]
The last element of the list that we've removed using the pop method:  66
list_example after sorting:  [11, 11, 22, 22, 33, 33, 44, 55, 55, 66, 77, 77]
list_example after reversing:  [77, 77, 66, 55, 55, 44, 33, 33, 22, 22, 11, 11]
list_example after clearing all the elements in the list:
 []

How to find the smallest and largest element in a list?

You can use complex code to find the smallest and biggest elements in a list. However, why go for complex when you have a more straightforward and uncomplicated way? Programming is all about making our lives easier. That’s why, although we are discussing methods related to the LISTS in this post, we will use the Python functions min() and max() to get the smallest and largest elements in a list, as shown in the example below.

#program to find the smallest and biggest
#in a list

list1=[] #declares an empty list

num=int(input("How many elements do you want to enter? "))

for n in range(num):
    list1.append(int(input("Enter number {}: ".format(n+1))))

print("The list is: ",list1)
print("\nThe smallest element in the declared list is: {}".format(min(list1)))
print("\nThe largest element in the declared list is: {}".format(max(list1)))
Output

How many elements do you want to enter? 5
Enter number 1: 23
Enter number 2: 45
Enter number 3: 12
Enter number 4: 56
Enter number 5: 38
The list is:  [23, 45, 12, 56, 38]

The smallest element in the declared list is: 12

The largest element in the declared list is: 56

How to sort the elements in a list?

There are several algorithmic approaches to sorting elements in a list. One of the most popular ways is to use the bubble sort technique.

In the bubble sort scenarios, you would compare the first element with the second element and swap the largest element to the second position. You would do this to all the elements in several iterations, and eventually, the highest number comes at the end, and the smallest number gets placed at the beginning of the list.

#python program to sort a list using
#bubble sort technique

list1=[]
num=int(input("How many numbers do you want to enter? "))

for n in range(num):
    list1.append(int(input("Enter element number {}:  ".format(n+1))))

print("The initial list is: ",list1)

#let us now use the bubble sort technique
flag=False #flag becomes True when swapping happens
for i in range(num-1):
    for j in range(num-1-i):
        if list1[j] > list1[j+1]:
            temp=list1[j]
            list1[j]=list1[j+1]
            list1[j+1]=temp
            flag=True
    if flag==False:
        break
    else:
        flag=False

print("The sorted list using bubble sort: \n",list1)
Output

How many numbers do you want to enter? 5
Enter element number 1:  12
Enter element number 2:  34
Enter element number 3:  56
Enter element number 4:  24
Enter element number 5:  38
The initial list is:  [12, 34, 56, 24, 38]
The sorted list using bubble sort:
 [12, 24, 34, 38, 56]

We performed the sorting operation using the bubble sort technique in the program mentioned above. However, the same sorting operation can be done much easier using the in-built sort() method.

#Simple Python program to sort a list
#using built-in methods

list1=[]

num=int(input("How many numbers do you want to enter? "))

for i in range(num):
    list1.append(int(input("Enter the element {}: ".format(i+1))))

print("The original list before sorting is: ",list1)

list1.sort()
print("The list1 after sorting in ascending order: ",list1)

list1.sort(reverse=True)
print("The list1 after sorting in descending order: ",list1)
Output

#Simple Python program to sort a list
#using built-in methods

list1=[]

num=int(input("How many numbers do you want to enter? "))

for i in range(num):
    list1.append(int(input("Enter the element {}: ".format(i+1))))

print("The original list before sorting is: ",list1)

list1.sort()
print("The list1 after sorting in ascending order: ",list1)

list1.sort(reverse=True)
print("The list1 after sorting in descending order: ",list1)

You can notice that we performed the sorting operation much quickly using the in-built sort() method. It sorted the elements of the list in ascending order. To sort the same elements in descending order, we used list1.sort(reverse==True).

See how Python can make our lives simpler. In the next chapter, we will explore a few more operations that you can perform on the Python lists.

Scroll to Top