Python list built-in Functions/Methods

Python list built-in Functions/Methods

In the previous tutorial, you learned how to use in-built string methods in your Python program. But, In this post, you will learn everything about python built-in list methods/functions.

If you want to know more about python lists click here.

Python list built-in Functions/Methods

Python provides many in-built methods/functions for list, which is work with python list datatype. And you can modify and manipulate python lists by using built-in lists methods/functions of these pythons.

We have provided a list below. Almost all built-in lists methods/functions python are there:

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

The built-in lists methods/functions of Python are given above in the list. Understand those built-in lists methods/functions python with their definition and example.

And know how to use the built-in lists methods/functions of these python methods with the list.

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]

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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