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 functions such as sum, min, and max functions.
Python List Functions
The following are the list of Python List Functions that are available to work.
Python 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() | This method adds the new list to the end of an existing list. |
del | This 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 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)
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)
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)
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)
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)
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 = [5, 19, 10, 18, 22, 170, 90]
print("Original List Items are : ", number)
number.remove(22)
print("List Items after Removing 22 are : ", number)
number.remove(19)
print("List Items after Removing 19 are : ", number)
number.remove(5)
print("List Items after Removing 5 are : ", number)
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)
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)
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)
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)
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)
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)
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)