Python Central HubSign in

Python Programming / Python List

List Methods

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.

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.

List Methods

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.

Table of Methods

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

append()

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:

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

Output:

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

clear()

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

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

Output:

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

copy()

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

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

Output:

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

count()

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:

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

Output:

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

extend()

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:

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

Output:

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

index()

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:

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

Output:

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

insert()

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:

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

Output:

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

pop()

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

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

Output:

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

remove()

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:

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

Output:

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

reverse()

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

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

Output:

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

sort()

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

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

Output:

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

len()

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

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

Output:

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

max()

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

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

Output:

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

min()

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

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

Output:

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

sum()

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:

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

Output:

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

all()

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:

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

Output:

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

any()

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:

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

Output:

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

enumerate()

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:

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

Output:

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

filter()

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:

Python
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:

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

map()

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:

Python
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:

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

reversed()

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

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

Output:

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

sorted()

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

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

Output:

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

zip()

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:

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

Output:

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

del

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

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

Output:

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

in

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:

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

Output:

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

not in

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:

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

Output:

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

Conclusion

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.

Check yourself

quizCheck yourself
  1. What does `L.sort()` return?

    Show answer

    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?

    Show answer

    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?

    Show answer

    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?

    Show answer

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

Try it: List Methods Exercises

Exercise 1 – append() and remove()

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – sort() and reverse()

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – index() and count()

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did