Python List Functions

Python List is the powerful one to hold different kinds of items. In python, we have different kinds of list functions that can add, remove, sort, reverse the list items.

In this section, we explain to you the list of Python list functions with an example of each. We also include some common Python list functions such as sum, min, and max functions.

Python List Functions

The Python List Data type has separate functions or methods for adding, removing, finding, manipulating, and reversing list items. Please use these link to understand the Python list functions with examples. The following are the list of Python List Functions that are available to work.

Python List FunctionsDescription
append()It append or add item x to the end of a list.
insert()Inserts the specified item x at index position i
extend()This method adds the new list to the end of an existing list.
delThis Python list function removes the value at a specified index.
pop()Remove an item at a specified index and display the removed item. After removing, the remaining list items moved forward to fill the index gap.
remove()It removes the user-specified list from a given list. It is very useful, if we know the item.
copy()This Python List function shallow copies the list items into a new list
clear()It clears the existing list items.
count()Counts the number of times value is repeated inside a list
reverse()Reverse the items in a list
sort()Sort the list items in Ascending order
index()Prints the index position of a specified value in a list

Python List Functions Examples

The following are the available Python list functions examples

Python List append

Python append helps us to add items at the end of a list. This example adds 520, 650, and -70 to the number list.

TIP: Please refer to List article in python.

# Python List Functions

# Python List Append Function

number = [150, 200, 130, 340]

print("Original List Items are            : ", number)

number.append(520)
print("List Items after 520 appended are  : ", number)

number.append(650)
print("List Items after 650 appended are  : ", number)

number.append(-70)
print("List Items after -70 appended are  : ", number)

List append function output

Original List Items are            :  [150, 200, 130, 340]
List Items after 520 appended are  :  [150, 200, 130, 340, 520]
List Items after 650 appended are  :  [150, 200, 130, 340, 520, 650]
List Items after -70 appended are  :  [150, 200, 130, 340, 520, 650, -70]

Python List extend

The Python extend function adds the items in New_List at the end of a list. In this example, we declared the number, a, b, and c lists. Next, we used this function to add a, b, c list items to the number list.

# Python List Functions
# Python List Extend Function

number = [10, 200, 630, 90]

print("Original List Items are          : ", number)

a = [222, 333]
number.extend(a)
print("List Items after extending to a  : ", number)

b = [5, 9]
number.extend(b)
print("List Items after extending to b  : ", number)

c = [-12, 73]
number.extend(c)
print("List Items after extending to c  : ", number)

List extend function output

Original List Items are          :  [10, 200, 630, 90]
List Items after extending to a  :  [10, 200, 630, 90, 222, 333]
List Items after extending to b  :  [10, 200, 630, 90, 222, 333, 5, 9]
List Items after extending to c  :  [10, 200, 630, 90, 222, 333, 5, 9, -12, 73]

Python List insert function

Python insert function inserts the given item at a specified index position. The first statement inserts 100 at index position 2, and the second statement inserts 500 at position 4.

# Python List Functions

# Python List Insert Function

number = [5, 10, 15, 22, 19, 90]

print("Original List Items are              : ", number)

number.insert(2, 100)
print("List Items after Inserting 100 at 2  : ", number)

number.insert(4, 500)
print("List Items after Inserting 500 at 4  : ", number)

number.insert(8, 700)
print("List Items after Inserting 700 at 8  : ", number)

List insert function output

Original List Items are              :  [5, 10, 15, 22, 19, 90]
List Items after Inserting 100 at 2  :  [5, 10, 100, 15, 22, 19, 90]
List Items after Inserting 500 at 4  :  [5, 10, 100, 15, 500, 22, 19, 90]
List Items after Inserting 700 at 8  :  [5, 10, 100, 15, 500, 22, 19, 90, 700]

Python list del

The Python del function deletes the value at a specified index. This example deletes items at index positions 5, 0, and 3.

# Python List Functions

# Python List Del Function

number = [9, 17, 10, 18, 55, 120, 90]

print("Original List Items are                   : ", number)

del number[5]
print("List Items after Deleting Item at Index 5 : ", number)

del number[0]
print("List Items after Deleting Item at Index 0 : ", number)

del number[3]
print("List Items after Deleting Item at Index 3 : ", number)

List delete function output

Original List Items are                   :  [9, 17, 10, 18, 55, 120, 90]
List Items after Deleting Item at Index 5 :  [9, 17, 10, 18, 55, 90]
List Items after Deleting Item at Index 0 :  [17, 10, 18, 55, 90]
List Items after Deleting Item at Index 3 :  [17, 10, 18, 90]

Python List pop function

The Python pop function removes the items at the user given index and displays the removed element. After removing, the remaining values adjust to fill the index gap. The below program remove and displays the items at index position 6, 0, and 4.

# Python List Functions

# Python List Pop Function

number = [17, 6, 10, 18, 120, 220, 90, 119]

print("Original List Items are                   : ", number)

a = number.pop(6)
print("List Items after Deleting Item at Index 6 : ", number)
print("Items Extracted by the Pop Function       : ", a)

b = number.pop(0)
print("\nList Items after Deleting Item at Index 0 : ", number)
print("Items Extracted by the Pop Function       : ", b)

c = number.pop(4)
print("\nList Items after Deleting Item at Index 4 : ", number)
print("Items Extracted by the Pop Function       : ", c)

List pop function output

Original List Items are                   :  [17, 6, 10, 18, 120, 220, 90, 119]
List Items after Deleting Item at Index 6 :  [17, 6, 10, 18, 120, 220, 119]
Items Extracted by the Pop Function       :  90

List Items after Deleting Item at Index 0 :  [6, 10, 18, 120, 220, 119]
Items Extracted by the Pop Function       :  17

List Items after Deleting Item at Index 4 :  [6, 10, 18, 120, 119]
Items Extracted by the Pop Function       :  220

Python List remove method

If we know the List item, we can use python remove function to remove a list item. The below program removes number list items 22, 19, and 5.

# Python List Functions
 
# Python List Remove Function
 
number = [55, 98, 10, 18, 22, 162, 170, 90]
 
print("Original List Items are           : ", number)
 
number.remove(22)
print("List Items after Removing 22 are  : ", number)
 
number.remove(98)
print("List Items after Removing 98 are  : ", number)
 
number.remove(162)
print("List Items after Removing 162 are : ", number)

List remove function output

Original List Items are           :  [55, 98, 10, 18, 22, 162, 170, 90]
List Items after Removing 22 are  :  [55, 98, 10, 18, 162, 170, 90]
List Items after Removing 98 are  :  [55, 10, 18, 162, 170, 90]
List Items after Removing 162 are :  [55, 10, 18, 170, 90]

Python list copy

The Python List copy method shallow copies the list items into a completely new list.

# Python List Copy Function

numbers = [6, 10, 18, 220, 90, 119]

print("List Items are        : ", numbers)

new_list = numbers.copy()
print("\nNew List Items  are   : ", new_list)

List copy function output

List Items are        :  [6, 10, 18, 220, 90, 119]

New List Items  are   :  [6, 10, 18, 220, 90, 119]

Python list clear

Python List clear method helps you to clear all the existing list items. After executing this method, if you call or print the same list, it returns an empty list.

# Python List Clear Function

numbers = [6, 10, 18, 220, 90, 119]

print("List Items are        : ", numbers)

new_list = numbers.clear()
print("\nNew List Items  are   : ", new_list)

List clear function output

List Items are        :  [6, 10, 18, 220, 90, 119]

New List Items  are   :  None

Python list count

Python List count function counts the number of times a specified value repeated in a list. Here, we are counting how many times 22, 6, and 19 repeated in numbers list.

# Python List Count Function

numbers = [22, 6, 15, 19, 22, 90, 19, 22, 6, 19, 22]

print("List Items are        : ", numbers)

a = numbers.count(22)
print("Number of Times 22 was repeated   : ", a)

b = numbers.count(6)
print("Number of Times 6 was repeated    : ", b)

c = numbers.count(19)
print("Number of Times 19 was repeated   : ", c)

List count function output

List Items are        :  [22, 6, 15, 19, 22, 90, 19, 22, 6, 19, 22]
Number of Times 22 was repeated   :  4
Number of Times 6 was repeated    :  2
Number of Times 19 was repeated   :  3

Python list index

Python List index returns the index position of a user given value in a list. Here, we are finding the index position of a numbers list items 12, -9, and -19

# Python List Index Function

numbers = [22, 6, 12, 15, 19, 16, -9, 4]

print("List Items are        : ", numbers)

a = numbers.index(12)
print("Index Position of 12 in this List   : ", a)

b = numbers.index(-9)
print("Index Position of -9 in this List   : ", b)

c = numbers.index(19)
print("Index Position of 19 in this List   : ", c)

List index function output

List Items are        :  [22, 6, 12, 15, 19, 16, -9, 4]
Index Position of 12 in this List   :  2
Index Position of -9 in this List   :  6
Index Position of 19 in this List   :  4

Python list reverse

The Python List reverse method helps to reverse the items in a list. This code reverses the numbers list.

# Python List Reverse Function

numbers = [22, 6, 12, 15, 19, 16, -9, 4]

print("List Items are        : ", numbers)

numbers.reverse()
print("\nNew List Items are   : ", numbers)

List reverse function output

List Items are        :  [22, 6, 12, 15, 19, 16, -9, 4]

New List Items are   :  [4, -9, 16, 19, 15, 12, 6, 22]

Python list sort

The Python List sort method sorts the list items in Ascending order.

# Python List Sort Function

numbers = [2, 6, 0, 12, 15, -2, 19, 16, -9, 4]

print("List Items are        : ", numbers)

numbers.sort()
print("\nNew Sorted List Items are   : ", numbers)

List sort function output

List Items are        :  [2, 6, 0, 12, 15, -2, 19, 16, -9, 4]

New Sorted List Items are   :  [-9, -2, 0, 2, 4, 6, 12, 15, 16, 19]

Python List sum, min, max

The Python List sum returns the sum of all items available in a given list. Next, the Python List min returns the minimum value among the given list items and the List max returns the maximum value.

# Python List Sum, Min, Max Function

numbers = [2, 6, 17, 12, 15, -2, 25, 16, -9, 4]

print("List Items are        : ", numbers)

# Python List Max Function
maximum = max(numbers)
print("The Maximum Value in this List    : ", maximum)

# Python List MinFunction
minimum = min(numbers)
print("The Minimum Value in this List    : ", minimum)

# Python List Sum Function
total = sum(numbers)
print("The Sum of all Value in this List : ", total)
Python List Functions 13