List Methods
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.No | Method | Description | Example |
---|---|---|---|
1 | append() | Adds an element at the end of the list | list.append(element) list.append(element) |
2 | clear() | Removes all the elements from the list | list.clear() list.clear() |
3 | copy() | Returns a copy of the list | list.copy() list.copy() |
4 | count() | Returns the number of elements with the specified value | list.count(value) list.count(value) |
5 | extend() | Add the elements of a list (or any iterable), to the end of the current list | list.extend(iterable) list.extend(iterable) |
6 | index() | Returns the index of the first element with the specified value | list.index(value) list.index(value) |
7 | insert() | Adds an element at the specified position | list.insert(position, element) list.insert(position, element) |
8 | pop() | Removes the element at the specified position | list.pop(position) list.pop(position) |
9 | remove() | Removes the first item with the specified value | list.remove(value) list.remove(value) |
10 | reverse() | Reverses the order of the list | list.reverse() list.reverse() |
11 | sort() | Sorts the list | list.sort() list.sort() |
12 | len() | Returns the length of the list | len(list) len(list) |
13 | max() | Returns the largest item in the list | max(list) max(list) |
14 | min() | Returns the smallest item in the list | min(list) min(list) |
15 | sum() | Returns the sum of all items in the list | sum(list) sum(list) |
16 | all() | Returns True if all items in the list are true | all(list) all(list) |
17 | any() | Returns True if any item in the list is true | any(list) any(list) |
18 | enumerate() | Returns an enumerate object. It contains the index and value of all the items in the list | enumerate(list) enumerate(list) |
19 | filter() | Use a filter function to exclude items in an iterable object | filter(function, iterable) filter(function, iterable) |
20 | map() | Use a map function to execute a specified function for each item in an iterable. The item is sent to the function as a parameter | map(function, iterable) map(function, iterable) |
21 | reversed() | Returns a reversed iterator | reversed(list) reversed(list) |
22 | sorted() | Returns a sorted list | sorted(list) sorted(list) |
23 | zip() | Returns an iterator, from two or more iterators | zip(iterator1, iterator2, iterator3, ...) zip(iterator1, iterator2, iterator3, ...) |
24 | del | Removes the specified index | del list[index] del list[index] |
25 | in | Returns True if a sequence with the specified value is present in the list | element in list element in list |
26 | not in | Returns True if a sequence with the specified value is not present in the list | element not in list element not in list |
append()
The append()
append()
method adds an element at the end of the list. The syntax of the append()
append()
method is: list.append(element)
list.append(element)
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
fruits.append("orange")
print(fruits)
fruits = ['apple', 'banana', 'cherry']
fruits.append("orange")
print(fruits)
Output:
C:\Users\username>python append.py
['apple', 'banana', 'cherry', 'orange']
C:\Users\username>python append.py
['apple', 'banana', 'cherry', 'orange']
clear()
The clear()
clear()
method removes all the elements from the list. The syntax of the clear()
clear()
method is: list.clear()
list.clear()
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits)
fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits)
Output:
C:\Users\username>python clear.py
[]
C:\Users\username>python clear.py
[]
copy()
The copy()
copy()
method returns a copy of the list. The syntax of the copy()
copy()
method is: list.copy()
list.copy()
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
fruits_copy = fruits.copy()
print(fruits_copy)
fruits = ['apple', 'banana', 'cherry']
fruits_copy = fruits.copy()
print(fruits_copy)
Output:
C:\Users\username>python copy.py
['apple', 'banana', 'cherry']
C:\Users\username>python copy.py
['apple', 'banana', 'cherry']
count()
The count()
count()
method returns the number of elements with the specified value. The syntax of the count()
count()
method is: list.count(value)
list.count(value)
. Here is an example:
fruits = ['apple', 'banana', 'cherry', 'banana']
count = fruits.count("banana")
print(count)
fruits = ['apple', 'banana', 'cherry', 'banana']
count = fruits.count("banana")
print(count)
Output:
C:\Users\username>python count.py
2
C:\Users\username>python count.py
2
extend()
The extend()
extend()
method adds the elements of a list (or any iterable), to the end of the current list. The syntax of the extend()
extend()
method is: list.extend(iterable)
list.extend(iterable)
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
more_fruits = ['orange', 'mango', 'grapes']
fruits.extend(more_fruits)
print(fruits)
fruits = ['apple', 'banana', 'cherry']
more_fruits = ['orange', 'mango', 'grapes']
fruits.extend(more_fruits)
print(fruits)
Output:
C:\Users\username>python extend.py
['apple', 'banana', 'cherry', 'orange', 'mango', 'grapes']
C:\Users\username>python extend.py
['apple', 'banana', 'cherry', 'orange', 'mango', 'grapes']
index()
The index()
index()
method returns the index of the first element with the specified value. The syntax of the index()
index()
method is: list.index(value)
list.index(value)
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
index = fruits.index("banana")
print(index)
fruits = ['apple', 'banana', 'cherry']
index = fruits.index("banana")
print(index)
Output:
C:\Users\username>python index.py
1
C:\Users\username>python index.py
1
insert()
The insert()
insert()
method adds an element at the specified position. The syntax of the insert()
insert()
method is: list.insert(position, element)
list.insert(position, element)
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)
Output:
C:\Users\username>python insert.py
['apple', 'orange', 'banana', 'cherry']
C:\Users\username>python insert.py
['apple', 'orange', 'banana', 'cherry']
pop()
The pop()
pop()
method removes the element at the specified position. The syntax of the pop()
pop()
method is: list.pop(position)
list.pop(position)
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
Output:
C:\Users\username>python pop.py
['apple', 'cherry']
C:\Users\username>python pop.py
['apple', 'cherry']
remove()
The remove()
remove()
method removes the first item with the specified value. The syntax of the remove()
remove()
method is: list.remove(value)
list.remove(value)
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
fruits.remove("banana")
print(fruits)
fruits = ['apple', 'banana', 'cherry']
fruits.remove("banana")
print(fruits)
Output:
C:\Users\username>python remove.py
['apple', 'cherry']
C:\Users\username>python remove.py
['apple', 'cherry']
reverse()
The reverse()
reverse()
method reverses the order of the list. The syntax of the reverse()
reverse()
method is: list.reverse()
list.reverse()
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)
Output:
C:\Users\username>python reverse.py
['cherry', 'banana', 'apple']
C:\Users\username>python reverse.py
['cherry', 'banana', 'apple']
sort()
The sort()
sort()
method sorts the list. The syntax of the sort()
sort()
method is: list.sort()
list.sort()
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
fruits.sort()
print(fruits)
fruits = ['apple', 'banana', 'cherry']
fruits.sort()
print(fruits)
Output:
C:\Users\username>python sort.py
['apple', 'banana', 'cherry']
C:\Users\username>python sort.py
['apple', 'banana', 'cherry']
len()
The len()
len()
method returns the length of the list. The syntax of the len()
len()
method is: len(list)
len(list)
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
length = len(fruits)
print(length)
fruits = ['apple', 'banana', 'cherry']
length = len(fruits)
print(length)
Output:
C:\Users\username>python len.py
3
C:\Users\username>python len.py
3
max()
The max()
max()
method returns the largest item in the list. The syntax of the max()
max()
method is: max(list)
max(list)
. Here is an example:
numbers = [1, 2, 3, 4, 5]
max_number = max(numbers)
print(max_number)
numbers = [1, 2, 3, 4, 5]
max_number = max(numbers)
print(max_number)
Output:
C:\Users\username>python max.py
5
C:\Users\username>python max.py
5
min()
The min()
min()
method returns the smallest item in the list. The syntax of the min()
min()
method is: min(list)
min(list)
. Here is an example:
numbers = [1, 2, 3, 4, 5]
min_number = min(numbers)
print(min_number)
numbers = [1, 2, 3, 4, 5]
min_number = min(numbers)
print(min_number)
Output:
C:\Users\username>python min.py
1
C:\Users\username>python min.py
1
sum()
The sum()
sum()
method returns the sum of all items in the list. The syntax of the sum()
sum()
method is: sum(list)
sum(list)
. Here is an example:
numbers = [1, 2, 3, 4, 5]
sum_numbers = sum(numbers)
print(sum_numbers)
numbers = [1, 2, 3, 4, 5]
sum_numbers = sum(numbers)
print(sum_numbers)
Output:
C:\Users\username>python sum.py
15
C:\Users\username>python sum.py
15
all()
The all()
all()
method returns True if all items in the list are true. The syntax of the all()
all()
method is: all(list)
all(list)
. Here is an example:
numbers = [1, 2, 3, 4, 5]
all_numbers = all(numbers)
print(all_numbers)
numbers = [1, 2, 3, 4, 5]
all_numbers = all(numbers)
print(all_numbers)
Output:
C:\Users\username>python all.py
True
C:\Users\username>python all.py
True
any()
The any()
any()
method returns True if any item in the list is true. The syntax of the any()
any()
method is: any(list)
any(list)
. Here is an example:
numbers = [1, 2, 3, 4, 5]
any_numbers = any(numbers)
print(any_numbers)
numbers = [1, 2, 3, 4, 5]
any_numbers = any(numbers)
print(any_numbers)
Output:
C:\Users\username>python any.py
True
C:\Users\username>python any.py
True
enumerate()
The enumerate()
enumerate()
method returns an enumerate object. It contains the index and value of all the items in the list. The syntax of the enumerate()
enumerate()
method is: enumerate(list)
enumerate(list)
. Here is an example:
numbers = [1, 2, 3, 4, 5]
enumerate_numbers = enumerate(numbers)
print(enumerate_numbers)
print(list(enumerate_numbers))
numbers = [1, 2, 3, 4, 5]
enumerate_numbers = enumerate(numbers)
print(enumerate_numbers)
print(list(enumerate_numbers))
Output:
C:\Users\username>python enumerate.py
<enumerate object at 0x0000020F7F7F4F00>
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
C:\Users\username>python enumerate.py
<enumerate object at 0x0000020F7F7F4F00>
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
filter()
The filter()
filter()
method uses a filter function to exclude items in an iterable object. The syntax of the filter()
filter()
method is: filter(function, iterable)
filter(function, iterable)
. Here is an example:
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))
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:
C:\Users\username>python filter.py
<filter object at 0x0000020F7F7F4F00>
[2, 4]
C:\Users\username>python filter.py
<filter object at 0x0000020F7F7F4F00>
[2, 4]
map()
The map()
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()
map()
method is: map(function, iterable)
map(function, iterable)
. Here is an example:
def square(number):
return number * number
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
print(squared_numbers)
print(list(squared_numbers))
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:
C:\Users\username>python map.py
<map object at 0x0000020F7F7F4F00>
[1, 4, 9, 16, 25]
C:\Users\username>python map.py
<map object at 0x0000020F7F7F4F00>
[1, 4, 9, 16, 25]
reversed()
The reversed()
reversed()
method returns a reversed iterator. The syntax of the reversed()
reversed()
method is: reversed(list)
reversed(list)
. Here is an example:
numbers = [1, 2, 3, 4, 5]
reversed_numbers = reversed(numbers)
print(reversed_numbers)
print(list(reversed_numbers))
numbers = [1, 2, 3, 4, 5]
reversed_numbers = reversed(numbers)
print(reversed_numbers)
print(list(reversed_numbers))
Output:
C:\Users\username>python reversed.py
<list_reverseiterator object at 0x0000020F7F7F4F00>
[5, 4, 3, 2, 1]
C:\Users\username>python reversed.py
<list_reverseiterator object at 0x0000020F7F7F4F00>
[5, 4, 3, 2, 1]
sorted()
The sorted()
sorted()
method returns a sorted list. The syntax of the sorted()
sorted()
method is: sorted(list)
sorted(list)
. Here is an example:
numbers = [5, 2, 3, 1, 4]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
numbers = [5, 2, 3, 1, 4]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
Output:
C:\Users\username>python sorted.py
[1, 2, 3, 4, 5]
C:\Users\username>python sorted.py
[1, 2, 3, 4, 5]
zip()
The zip()
zip()
method returns an iterator, from two or more iterators. The syntax of the zip()
zip()
method is: zip(iterator1, iterator2, iterator3, ...)
zip(iterator1, iterator2, iterator3, ...)
. Here is an example:
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']
zipped = zip(numbers, fruits)
print(zipped)
print(list(zipped))
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']
zipped = zip(numbers, fruits)
print(zipped)
print(list(zipped))
Output:
C:\Users\username>python zip.py
<zip object at 0x0000020F7F7F4F00>
[(1, 'apple'), (2, 'banana'), (3, 'cherry')]
C:\Users\username>python zip.py
<zip object at 0x0000020F7F7F4F00>
[(1, 'apple'), (2, 'banana'), (3, 'cherry')]
del
The del
del
keyword removes the specified index. The syntax of the del
del
keyword is: del list[index]
del list[index]
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
del fruits[1]
print(fruits)
fruits = ['apple', 'banana', 'cherry']
del fruits[1]
print(fruits)
Output:
C:\Users\username>python del.py
['apple', 'cherry']
C:\Users\username>python del.py
['apple', 'cherry']
in
The in
in
keyword returns True if a sequence with the specified value is present in the list. The syntax of the in
in
keyword is: element in list
element in list
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
print("banana" in fruits)
print("orange" in fruits)
fruits = ['apple', 'banana', 'cherry']
print("banana" in fruits)
print("orange" in fruits)
Output:
C:\Users\username>python in.py
True
False
C:\Users\username>python in.py
True
False
not in
The not in
not in
keyword returns True if a sequence with the specified value is not present in the list. The syntax of the not in
not in
keyword is: element not in list
element not in list
. Here is an example:
fruits = ['apple', 'banana', 'cherry']
print("banana" not in fruits)
print("orange" not in fruits)
fruits = ['apple', 'banana', 'cherry']
print("banana" not in fruits)
print("orange" not in fruits)
Output:
C:\Users\username>python not-in.py
False
True
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.
Was this page helpful?
Let us know how we did