List methods in Python

Python List methods; Through this tutorial, i am going to show you about built-in list methods/functions in python.

Using the python list methods; you can insert/add, remove/delete, modify, sort, copy, count, and reverse list or array element in python.

Python list built-in Functions/Methods

See the built-in lists methods/functions in python; as shown in below list:

MethodDescription
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

See the uses of built-in list methods/functions in python; as shown below:

Python append method

The append() function is used to add items to the Python list. And the item is added to the end of the list.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.append(100)
print(my_list)
#Output
#[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Python clear list method

The python clear() method is used to removes all the elements from a list.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.clear()
print(my_list)
#Output
#[]

List copy method in python

The copy() function copies one list into another list.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
blist= my_list.copy()
print(blist)
#output
# [10, 20, 30, 40, 50, 60, 70, 80, 90]

List count method in list in python

The count() method returns the number of elements with the specified value.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90,10, 20, 10]
blist= my_list.count(10)
print(blist)
#output
# 3

List extend method in python

In Python, the Extend () method is used to add specific list items to the current list.

a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a)
#ouput
#[1, 2, 3, 4, 5, 6]

List index method in python

 The index() method is used to find the first occurrence of the specified value.

a = [1, 2, 3, 2, 5, 6, 7, 2]
x = a.index(2)
print(x)
#output
#1

Python List insert() Method

The insert() method is used to insert the specified element at the specified position in the python list.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.insert(1, 5)
print(my_list)
#Output
#[10, 5, 20, 30, 40, 50, 60, 70, 80, 90]

List pop() Method in Python

The pop() method used to remove the element at the specified position in python list.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.pop(2)
print(my_list)
#[10, 20, 40, 50, 60, 70, 80, 90] 

List remove method in python

The remove() method is used to remove the first occurrence item with a specified value on python lists.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.remove(90)
print(my_list)
#output
# [10, 20, 30, 40, 50, 60, 70, 80] 

Python list reverse method

The reverse() method is used to reverse the sorting order of the elements.

x = [1, 2, 3, 4, 5]
​
x.reverse()
​
print(x)
​
#Output
#[5, 4, 3, 2, 1]

Python sort list method

The sort() method sorts the list ascending by default.

x = [6, 7, 1, 5, 4, 8]
x.sort()
print(x)
#Output
#[1, 4, 5, 6, 7, 8]

Recommended Python Tutorials

Recommended:-Functions in Python
Recommended:-Python Modules
Recommended:-Python Lists
Recommended:-Python Strings

Be First to Comment

Leave a Reply

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