Python List Functions

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

In this section, we explain to you the available list functions with an example of each. We also include some common Python list methods 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 elements. Please use these links to understand the list functions with examples.

The following are the Python list methods or functions that are available to work.

FunctionsDescription
append()It appends or adds item x to the end of a list.
insert()Inserts the specified item x at index position i
extend()It adds the new to the end of an existing one.
delThis Python list method or 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 items moved forward to fill the index gap.
remove()It removes the user-specified item. It is very useful if we know the item.
copy()This will shallow copies the items into a new one.
clear()It clears the existing elements.
count()Counts the number of times the value is repeated inside it.
reverse()This method reverses the items
sort()Sort the items in Ascending order
index()The index method prints the index position of a specified value.

Python List Functions Examples

The following are the available Python list functions examples

Python list append method

The append helps us to add items at the end. This example adds 520, 650, and -70 to the numbers.

TIP: Please refer to the List article in Python.

number = [150, 200, 130, 340]
 
print("Original Items are      : ", number)
 
number.append(520)
print("After 520 appended are  : ", number)
 
number.append(650)
print("After 650 appended are  : ", number)
 
number.append(-70)
print("After -70 appended are  : ", number)
Original Items are      :  [150, 200, 130, 340]
After 520 appended are  :  [150, 200, 130, 340, 520]
After 650 appended are  :  [150, 200, 130, 340, 520, 650]
After -70 appended are  :  [150, 200, 130, 340, 520, 650, -70]

Python list extend function

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

number1 = [10, 200, 630, 90]
 
print("Original Items are      : ", number1)
 
a = [222, 333]
number1.extend(a)
print("After extending to a  : ", number1)
 
b = [5, 9]
number1.extend(b)
print("After extending to b  : ", number1)
 
c = [-12, 73]
number1.extend(c)
print("After extending to c  : ", number1)
Original Items are    :  [10, 200, 630, 90]
After extending to a  :  [10, 200, 630, 90, 222, 333]
After extending to b  :  [10, 200, 630, 90, 222, 333, 5, 9]
After extending to c  :  [10, 200, 630, 90, 222, 333, 5, 9, -12, 73]

insert

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

number2 = [5, 10, 15, 22, 19, 90]
 
print("Original Items are           : ", number2)
 
number2.insert(2, 100)
print("After Inserting 100 at 2  : ", number2)
 
number2.insert(4, 500)
print("After Inserting 500 at 4  : ", number2)
 
number2.insert(8, 700)
print("After Inserting 700 at 8  : ", number2)
Original Items are        :  [5, 10, 15, 22, 19, 90]
After Inserting 100 at 2  :  [5, 10, 100, 15, 22, 19, 90]
After Inserting 500 at 4  :  [5, 10, 100, 15, 500, 22, 19, 90]
After Inserting 700 at 8  :  [5, 10, 100, 15, 500, 22, 19, 90, 700]

Python list del function

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

number3 = [9, 17, 10, 18, 55, 120, 90]
 
print("Original Items are                      : ", number3)
 
del number3[5]
print("After Deleting Item at Index 5 : ", number3)
 
del number3[0]
print("After Deleting Item at Index 0 : ", number3)
 
del number3[3]
print("After Deleting Item at Index 3 : ", number3)
Original Items are                   :  [9, 17, 10, 18, 55, 120, 90]
After Deleting Item at Index 5 :  [9, 17, 10, 18, 55, 90]
After Deleting Item at Index 0 :  [17, 10, 18, 55, 90]
After Deleting Item at Index 3 :  [17, 10, 18, 90]

Python list pop function

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

number4 = [17, 6, 10, 18, 120, 220, 90, 119]
 
print("Original Items are                   : ", number4)
 
a = number4.pop(6)
print("After Deleting Item at Index 6 : ", number4)
print("Items Extracted by the Pop Function       : ", a)
 
b = number4.pop(0)
print("\nAfter Deleting Item at Index 0 : ", number4)
print("Items Extracted by the Pop Function       : ", b)
 
c = number4.pop(4)
print("\nAfter Deleting Item at Index 4 : ", number4)
print("Items Extracted by the Pop Function       : ", c)
Original Items are                   :  [17, 6, 10, 18, 120, 220, 90, 119]
After Deleting Item at Index 6 :  [17, 6, 10, 18, 120, 220, 119]
Items Extracted by the Pop Function       :  90

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

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

Python List remove function

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

number5 = [55, 98, 10, 18, 22, 162, 170, 90]
 
print("Original Items are           : ", number5)
 
number5.remove(22)
print("After Removing 22 are  : ", number5)
 
number5.remove(98)
print("After Removing 98 are  : ", number5)
 
number5.remove(162)
print("After Removing 162 are : ", number5)
Original Items are     :  [55, 98, 10, 18, 22, 162, 170, 90]
After Removing 22 are  :  [55, 98, 10, 18, 162, 170, 90]
After Removing 98 are  :  [55, 10, 18, 162, 170, 90]
After Removing 162 are :  [55, 10, 18, 170, 90]

list copy

The copy functions shallow copies the items into a completely new one.

numbers6 = [6, 10, 18, 220, 90, 119]
 
print("Items are        : ", numbers6)

newNum = numbers6.copy()
print("\nNew Items  are   : ", newNum)
Items are        :  [6, 10, 18, 220, 90, 119]

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

List clear

The clear function helps you to clear all the existing items. After executing this function, it returns an empty if you call or print the same one.

numbers7 = [6, 10, 18, 220, 90, 119]
 
print("Items are        : ", numbers7)
 
new = numbers7.clear()
print("\nNew Items  are   : ", new)
Items are        :  [6, 10, 18, 220, 90, 119]

New Items  are   :  None

How to count the items in a list?

The List count function counts the number of times a specified value is repeated. Here, we count how many times 22, 6, and 19 are repeated.

numbers8 = [22, 6, 15, 19, 22, 90, 19, 22, 6, 19, 22]
 
print(numbers8)
 
a = numbers8.count(22)
print("Number of Times 22 was repeated   : ", a)
 
b = numbers8.count(6)
print("Number of Times 6 was repeated    : ", b)
 
c = numbers8.count(19)
print("Number of Times 19 was repeated   : ", c)
[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

list index

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

numbers = [22, 6, 12, 15, 19, 16, -9, 4]
 
print("Items are         : ", numbers)
 
a = numbers.index(12)
print("Index Position of 12   : ", a)
 
b = numbers.index(-9)
print("Index Position of -9   : ", b)
 
c = numbers.index(19)
print("Index Position of 19   : ", c)
Items are        :  [22, 6, 12, 15, 19, 16, -9, 4]
Index Position of 12  :  2
Index Position of -9  :  6
Index Position of 19  :  4

How to reverse the list items?

The List reverse method helps to reverse the items. This code reverses the numbers.

numbers = [22, 6, 12, 15, 19, 16, -9, 4]
 
print("Items are             : ", numbers)
 
numbers.reverse()
print("\nNew Items are : ", numbers)
Items are       :  [22, 6, 12, 15, 19, 16, -9, 4]

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

sort list items

The sort method sorts the list items in Ascending order.

numb = [2, 6, 0, 12, 15, -2, 19, 16, -9, 4]
print(numb)

numb.sort()
print("\nSorted   : ", numb)
[2, 6, 0, 12, 15, -2, 19, 16, -9, 4]

Sorted :  [-9, -2, 0, 2, 4, 6, 12, 15, 16, 19]

This example shows the Python list methods sum, min, and max. The sum returns the sum of all items available in a given one. Next, the min returns the minimum value among the given items, and the max returns the maximum value.

numbers = [2, 6, 17, 12, 15, -2, 25, 16, -9, 4]
 
print("Items are        : ", numbers)
 
# Max 
maximum = max(numbers)
print("The Maximum Value in this    : ", maximum)
 
# Min
minimum = min(numbers)
print("The Minimum Value in this    : ", minimum)
 
# Sum
total = sum(numbers)
print("The Sum of all Value in this : ", total)
Python List Functions or methods 13