Skip to content

List Methods

diagram the in-place method returns None; the function returns a new object mermaid
Python separates the two deliberately, and the giveaway is the return value: a method that mutates hands back None so that assigning its result is an obvious mistake rather than a silent one. The pairs are sort/sorted, reverse/reversed, and append-style methods generally.

The list data type has a set of built-in methods that you can use on lists. In this tutorial, you will learn how to use list methods in Python. Here you will find a list of all the methods that you can use with lists. you will also find examples of how to use them. you can find insert, append, extend, remove, pop, clear, index, count, sort, reverse, copy, and more.

There are many methods that you can use with lists. Here is a list of all the methods that you can use with lists.

S.NoMethodDescriptionExample
1append()Adds an element at the end of the listlist.append(element)
2clear()Removes all the elements from the listlist.clear()
3copy()Returns a copy of the listlist.copy()
4count()Returns the number of elements with the specified valuelist.count(value)
5extend()Add the elements of a list (or any iterable), to the end of the current listlist.extend(iterable)
6index()Returns the index of the first element with the specified valuelist.index(value)
7insert()Adds an element at the specified positionlist.insert(position, element)
8pop()Removes the element at the specified positionlist.pop(position)
9remove()Removes the first item with the specified valuelist.remove(value)
10reverse()Reverses the order of the listlist.reverse()
11sort()Sorts the listlist.sort()
12len()Returns the length of the listlen(list)
13max()Returns the largest item in the listmax(list)
14min()Returns the smallest item in the listmin(list)
15sum()Returns the sum of all items in the listsum(list)
16all()Returns True if all items in the list are trueall(list)
17any()Returns True if any item in the list is trueany(list)
18enumerate()Returns an enumerate object. It contains the index and value of all the items in the listenumerate(list)
19filter()Use a filter function to exclude items in an iterable objectfilter(function, iterable)
20map()Use a map function to execute a specified function for each item in an iterable. The item is sent to the function as a parametermap(function, iterable)
21reversed()Returns a reversed iteratorreversed(list)
22sorted()Returns a sorted listsorted(list)
23zip()Returns an iterator, from two or more iteratorszip(iterator1, iterator2, iterator3, ...)
24delRemoves the specified indexdel list[index]
25inReturns True if a sequence with the specified value is present in the listelement in list
26not inReturns True if a sequence with the specified value is not present in the listelement not in list

The append() method adds an element at the end of the list. The syntax of the append() method is: list.append(element). Here is an example:

append.py
fruits = ['apple', 'banana', 'cherry']
fruits.append("orange")
print(fruits)

Output:

command
C:\Users\username>python append.py
['apple', 'banana', 'cherry', 'orange']

The clear() method removes all the elements from the list. The syntax of the clear() method is: list.clear(). Here is an example:

clear.py
fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits)

Output:

command
C:\Users\username>python clear.py
[]

The copy() method returns a copy of the list. The syntax of the copy() method is: list.copy(). Here is an example:

copy.py
fruits = ['apple', 'banana', 'cherry']
fruits_copy = fruits.copy()
print(fruits_copy)

Output:

command
C:\Users\username>python copy.py
['apple', 'banana', 'cherry']

The count() method returns the number of elements with the specified value. The syntax of the count() method is: list.count(value). Here is an example:

count.py
fruits = ['apple', 'banana', 'cherry', 'banana']
count = fruits.count("banana")
print(count)

Output:

command
C:\Users\username>python count.py
2

The extend() method adds the elements of a list (or any iterable), to the end of the current list. The syntax of the extend() method is: list.extend(iterable). Here is an example:

extend.py
fruits = ['apple', 'banana', 'cherry']
more_fruits = ['orange', 'mango', 'grapes']
fruits.extend(more_fruits)
print(fruits)

Output:

command
C:\Users\username>python extend.py
['apple', 'banana', 'cherry', 'orange', 'mango', 'grapes']

The index() method returns the index of the first element with the specified value. The syntax of the index() method is: list.index(value). Here is an example:

index.py
fruits = ['apple', 'banana', 'cherry']
index = fruits.index("banana")
print(index)

Output:

command
C:\Users\username>python index.py
1

The insert() method adds an element at the specified position. The syntax of the insert() method is: list.insert(position, element). Here is an example:

insert.py
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)

Output:

command
C:\Users\username>python insert.py
['apple', 'orange', 'banana', 'cherry']

The pop() method removes the element at the specified position. The syntax of the pop() method is: list.pop(position). Here is an example:

pop.py
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)

Output:

command
C:\Users\username>python pop.py
['apple', 'cherry']

The remove() method removes the first item with the specified value. The syntax of the remove() method is: list.remove(value). Here is an example:

remove.py
fruits = ['apple', 'banana', 'cherry']
fruits.remove("banana")
print(fruits)

Output:

command
C:\Users\username>python remove.py
['apple', 'cherry']

The reverse() method reverses the order of the list. The syntax of the reverse() method is: list.reverse(). Here is an example:

reverse.py
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)

Output:

command
C:\Users\username>python reverse.py
['cherry', 'banana', 'apple']

The sort() method sorts the list. The syntax of the sort() method is: list.sort(). Here is an example:

sort.py
fruits = ['apple', 'banana', 'cherry']
fruits.sort()
print(fruits)

Output:

command
C:\Users\username>python sort.py
['apple', 'banana', 'cherry']

The len() method returns the length of the list. The syntax of the len() method is: len(list). Here is an example:

len.py
fruits = ['apple', 'banana', 'cherry']
length = len(fruits)
print(length)

Output:

command
C:\Users\username>python len.py
3

The max() method returns the largest item in the list. The syntax of the max() method is: max(list). Here is an example:

max.py
numbers = [1, 2, 3, 4, 5]
max_number = max(numbers)
print(max_number)

Output:

command
C:\Users\username>python max.py
5

The min() method returns the smallest item in the list. The syntax of the min() method is: min(list). Here is an example:

min.py
numbers = [1, 2, 3, 4, 5]
min_number = min(numbers)
print(min_number)

Output:

command
C:\Users\username>python min.py
1

The sum() method returns the sum of all items in the list. The syntax of the sum() method is: sum(list). Here is an example:

sum.py
numbers = [1, 2, 3, 4, 5]
sum_numbers = sum(numbers)
print(sum_numbers)

Output:

command
C:\Users\username>python sum.py
15

The all() method returns True if all items in the list are true. The syntax of the all() method is: all(list). Here is an example:

all.py
numbers = [1, 2, 3, 4, 5]
all_numbers = all(numbers)
print(all_numbers)

Output:

command
C:\Users\username>python all.py
True

The any() method returns True if any item in the list is true. The syntax of the any() method is: any(list). Here is an example:

any.py
numbers = [1, 2, 3, 4, 5]
any_numbers = any(numbers)
print(any_numbers)

Output:

command
C:\Users\username>python any.py
True

The enumerate() method returns an enumerate object. It contains the index and value of all the items in the list. The syntax of the enumerate() method is: enumerate(list). Here is an example:

enumerate.py
numbers = [1, 2, 3, 4, 5]
enumerate_numbers = enumerate(numbers)
print(enumerate_numbers)
print(list(enumerate_numbers))

Output:

command
C:\Users\username>python enumerate.py
<enumerate object at 0x0000020F7F7F4F00>
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

The filter() method uses a filter function to exclude items in an iterable object. The syntax of the filter() method is: filter(function, iterable). Here is an example:

filter.py
def is_even(number):
    return number % 2 == 0
 
numbers = [1, 2, 3, 4, 5]
even_numbers = filter(is_even, numbers)
print(even_numbers)
print(list(even_numbers))

Output:

command
C:\Users\username>python filter.py
<filter object at 0x0000020F7F7F4F00>
[2, 4]

The map() method uses a map function to execute a specified function for each item in an iterable. The item is sent to the function as a parameter. The syntax of the map() method is: map(function, iterable). Here is an example:

map.py
def square(number):
    return number * number
 
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
print(squared_numbers)
print(list(squared_numbers))

Output:

command
C:\Users\username>python map.py
<map object at 0x0000020F7F7F4F00>
[1, 4, 9, 16, 25]

The reversed() method returns a reversed iterator. The syntax of the reversed() method is: reversed(list). Here is an example:

reversed.py
numbers = [1, 2, 3, 4, 5]
reversed_numbers = reversed(numbers)
print(reversed_numbers)
print(list(reversed_numbers))

Output:

command
C:\Users\username>python reversed.py
<list_reverseiterator object at 0x0000020F7F7F4F00>
[5, 4, 3, 2, 1]

The sorted() method returns a sorted list. The syntax of the sorted() method is: sorted(list). Here is an example:

sorted.py
numbers = [5, 2, 3, 1, 4]
sorted_numbers = sorted(numbers)
print(sorted_numbers)

Output:

command
C:\Users\username>python sorted.py
[1, 2, 3, 4, 5]

The zip() method returns an iterator, from two or more iterators. The syntax of the zip() method is: zip(iterator1, iterator2, iterator3, ...). Here is an example:

zip.py
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']
zipped = zip(numbers, fruits)
print(zipped)
print(list(zipped))

Output:

command
C:\Users\username>python zip.py
<zip object at 0x0000020F7F7F4F00>
[(1, 'apple'), (2, 'banana'), (3, 'cherry')]

The del keyword removes the specified index. The syntax of the del keyword is: del list[index]. Here is an example:

del.py
fruits = ['apple', 'banana', 'cherry']
del fruits[1]
print(fruits)

Output:

command
C:\Users\username>python del.py
['apple', 'cherry']

The in keyword returns True if a sequence with the specified value is present in the list. The syntax of the in keyword is: element in list. Here is an example:

in.py
fruits = ['apple', 'banana', 'cherry']
print("banana" in fruits)
print("orange" in fruits)

Output:

command
C:\Users\username>python in.py
True
False

The not in keyword returns True if a sequence with the specified value is not present in the list. The syntax of the not in keyword is: element not in list. Here is an example:

not-in.py
fruits = ['apple', 'banana', 'cherry']
print("banana" not in fruits)
print("orange" not in fruits)

Output:

command
C:\Users\username>python not-in.py
False
True

In this tutorial, you learned how to use list methods in Python. Here you found a list of all the methods that you can use with lists. you also found examples of how to use them. you can find insert, append, extend, remove, pop, clear, index, count, sort, reverse, copy, and more. Now you can use these methods in your Python programs. For more information, visit the official Python documentation.


sketch Stable sorting, and why it lets you sort twice p5.js
Three records tied on the sort key. A stable sort keeps tied items in the order they arrived, which is what makes multi-key sorting work by sorting more than once -- least significant key first. Watch the two tied rows: they never swap. The result shown is the measured output.
pch.quizTag pch.quizDefaultTitle
  1. What does `L.sort()` return?

    pch.quizShowAnswer

    B — `None` — It mutates and returns None, by the same convention as `list.reverse()` and `set.update()`. `L = L.sort()` therefore sets `L` to None.

  2. Sorting `[('b',1), ('a',1), ('c',0)]` by the number gives what?

    pch.quizShowAnswer

    B — `[('c',0), ('b',1), ('a',1)]` — Verified. Python's sort is stable, so `('b',1)` stays ahead of `('a',1)` because their keys tie and that was their input order.

  3. How do you sort by department, then by name within each department?

    pch.quizShowAnswer

    A — Sort by name, then by department — Least significant key FIRST. Stability preserves the name order inside each group when the second sort reorders by department.

  4. What does `reversed([1,2,3])` return?

    pch.quizShowAnswer

    C — An iterator — It is lazy — wrap it in `list()` to materialise it. `L.reverse()` is the in-place counterpart and returns None.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading