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 items.

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

Python List methods

The Python List Data type has separate functions or methods for adding, removing, finding, manipulating, and reversing elements. Please use these link to understand the Python list functions with examples.

The following are the Python List methods that are available to work.

FunctionsPython List Functions Description
append()It append or add 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 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 Python List function shallow copies the items into a new one.
clear()It clears the existing elements.
count()Counts the number of times value is repeated inside it.
reverse()This method reverse the items
sort()Sort the items in Ascending order
index()The index method prints the index position of a specified value.

Python List methods Examples

The following are the available Python list functions examples

append method

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

TIP: Please refer to 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]

extend

The extend function adds the items in 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, 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 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 Python del function 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]

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.

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 method

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]

copy

The copy function 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]

Python List clear method

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

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

count

The List count function counts the number of times a specified value repeated. Here, we are counting how many times 22, 6, and 19 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

index

The Python List index method returns the index position of a user given value. Here, we are finding the index position of an 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

Python list reverse function

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

The Python 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 Python sum returns the sum of all items available in a given one. Next, the Python 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 13