Python List Methods and Functions with Examples

List Methods and Functions in Python

Python has some list methods and built-in functions that you can use on lists.

1. List Methods

The following is a list of Python list methods:

MethodDescription
append(item)Adds an element to the end of the list
--------------------------------
Example:
list = ['Apple', 'Grape', 'Melon']
list.append('Mango')
extend(iterable)Adds all elements of an iterable to the list
--------------------------------
Example:
list = ['Apple', 'Melon', 'Kiwi', 'Watermelon'];
sub_list = ['Apple', 'Grapefruit'];
list.extend(sub_list)
insert(index, item)Adds an element at a given position of the list
--------------------------------
Example:
list = ['Grapefruit', 'Melon', 'Kiwi']
list.insert(1, 'Apple')
remove(item)Removes the first occurrence of a given item from the list
--------------------------------
Example:
list = ['AAAA', 'BBBB', 'CCCC', 'BBBB', 'DDDD']
list.remove('BBBB')
pop([index])Removes the item at the specified position in the list and returns it.
And pop() removes and returns the last item of the list if index omitted.
--------------------------------
Example 1:
list1 = ['A', 'B', 'C', 'D']
item1 = list1.pop(1)


Example 2:
list2 = ['A', 'B', 'C', 'D'];
item2 = list2.pop()
clear()Removes all elements in the list
--------------------------------
Example:
list = ['A', 'B', 'C', 'D']
list.clear()
index(item[, start[, end]])Returns the index of the first occurrence of a given item
--------------------------------
Example:
list = ['Student A', 'Student B', 'Student C', 'Student D', 'Student B']
index_1 = list.index('Student B')
index_2 = list.index('Student B', 2, 5)
count(item)Counts the number of occurrences of a given element in a list.
--------------------------------
Example:
list = [1, 2, 3, 2, 2, 3, 2, 10, 2, 1, 2]
number = list.count(2)
sort(key=None, reverse=False)Sorts the items in the list ascending or descending
--------------------------------
Example 1:
list1 = [100, 200, 4000, 200, 200, 100, 300, 500]
list1.sort()


Example 2:
list2 = [100, 200, 4000, 200, 200, 100, 300, 500]
list2.sort(reverse = True)
reverse()Reverses the elements of the list in place
--------------------------------
Example:
list = [100, 200, 300, 400, 500, 600, 700, 800]
list.reverse()
copy()Returns a shallow copy of the list
--------------------------------
Example:
list1 = [100, 200, 300, 400, 500]
list2 = list1.copy()

2. List Functions

The following is a list of Python functions that can be used on lists:

FunctionDescription
all()Returns True if all items in a list are true, otherwise returns False
--------------------------------
Example:
all([1>2, False, 2==2, 3<2, True])
any()Returns True if any element of a given list is true, otherwise returns False
--------------------------------
Example:
any([1>2, False, 2==2, 3<2, False])
enumerate()Takes a list and convert it to an enumerate object
--------------------------------
Example:
fruits = ["Orange", "Apple", "Kiwi", "Melon", "Grapefruit"]
enumerate_object = enumerate(fruits)
for index, value in enumerate_object:
     print ("index:", index , " | value:" , value)
print ('------------------------------')
enumerate_object = enumerate(fruits, 2)
for index, value in enumerate_object:
    print ("index:", index , " | value:" , value) 
len()Returns the length of a given list (the number of characters in a given list, including blank spaces)
--------------------------------
Example:
len(["Student A", "Student B", "Student C", "Student D", "Student E"])
list()Takes an iterable and returns a list
--------------------------------
Example:
fruits_tuple = ("Melon", "Watermelon", "Orange", "Grapefruit", "Apple")
fruits_list = list(fruits_tuple)
max()Finds the maximum value in a list of numbers
--------------------------------
Example:
max([102, 200, 100, 300, 200, 120, 400, 450])
min()Find the smallest number in a list of numbers
--------------------------------
Example:
min([102, 200, 100, 300, 200, 120, 400, 450])
reversed()Takes a sequence and returns a reverse iterator
--------------------------------
Example:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
reversed_iterator = reversed(numbers)
reversed_numbers = list(reversed_iterator)
slice()Returns a slice object representing the set of indices specified by the startstop, and step arguments
-------------------------------
Example:
fruits = ["Orange", "Apple", "Kiwi", "Melon", "Grapefruit", "Watermelon"]

slice_object_n = slice(4)
fruits_n = fruits[slice_object_n]

slice_object_m = slice(0, 2)
fruits_m = fruits[slice_object_m]

slice_object_p = slice(1, 5, 2)
fruits_p = fruits[slice_object_p]
sorted()Takes an iterable and returns a sorted list
--------------------------------
Example:
numbers = [2010, 2100, 1100, 1300, 2200, 1120, 1400, 2450]
small_to_large_numbers = sorted(numbers)
large_to_small_numbers = sorted(numbers, reverse = True)
sum()Sums all numbers in a list
--------------------------------
Example:
sum([2010.50, 2010.00, 1200.00, 2003.00, 1230.50, 1200.50, 2450.00])

Reference: https://docs.python.org/3/tutorial/datastructures.html


See also:
Python return Statement with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python
Python max() Function – Find the Largest Value in Python

Leave a Comment