Python List Methods

Python provides built-in methods that allow you to add, remove, sort, and modify list items. List Methods Method Description append() Adds an item to the end of the list insert() Adds an item at a specified position extend() Adds elements from another iterable remove() Removes the first matching item pop() Removes an item at a […]

2 mins read Lesson 21 of 43 49% through track

Python provides built-in methods that allow you to add, remove, sort, and modify list items.

List Methods

MethodDescription
append()Adds an item to the end of the list
insert()Adds an item at a specified position
extend()Adds elements from another iterable
remove()Removes the first matching item
pop()Removes an item at a specified index
clear()Removes all items from the list
index()Returns the index of an item
count()Returns the number of occurrences
sort()Sorts the list
reverse()Reverses the list
copy()Returns a copy of the list

append()

Adds an item to the end of the list.

Example:

fruits = ["Apple", "Banana"]

fruits.append("Mango")

print(fruits)

Output:

['Apple', 'Banana', 'Mango']

insert()

Adds an item at a specific position.

Example:

fruits = ["Apple", "Mango"]

fruits.insert(1, "Banana")

print(fruits)

Output:

['Apple', 'Banana', 'Mango']

extend()

Adds items from another list.

Example:

fruits = ["Apple", "Banana"]
more_fruits = ["Mango", "Orange"]

fruits.extend(more_fruits)

print(fruits)

Output:

['Apple', 'Banana', 'Mango', 'Orange']

remove()

Removes the first matching item.

Example:

fruits = ["Apple", "Banana", "Mango"]

fruits.remove("Banana")

print(fruits)

Output:

['Apple', 'Mango']

pop()

Removes an item by index.

Example:

fruits = ["Apple", "Banana", "Mango"]

fruits.pop(1)

print(fruits)

Output:

['Apple', 'Mango']

If no index is specified, the last item is removed.

Example:

fruits = ["Apple", "Banana", "Mango"]

fruits.pop()

print(fruits)

Output:

['Apple', 'Banana']

clear()

Removes all items from the list.

Example:

fruits = ["Apple", "Banana", "Mango"]

fruits.clear()

print(fruits)

Output:

[]

index()

Returns the position of an item.

Example:

fruits = ["Apple", "Banana", "Mango"]

print(fruits.index("Banana"))

Output:

1

count()

Returns the number of times a value appears.

Example:

fruits = ["Apple", "Banana", "Apple"]

print(fruits.count("Apple"))

Output:

2

sort()

Sorts the list in ascending order.

Example:

numbers = [5, 2, 8, 1]

numbers.sort()

print(numbers)

Output:

[1, 2, 5, 8]

Sort strings alphabetically:

fruits = ["Mango", "Apple", "Banana"]

fruits.sort()

print(fruits)

Output:

['Apple', 'Banana', 'Mango']

sort(reverse=True)

Sorts the list in descending order.

Example:

numbers = [5, 2, 8, 1]

numbers.sort(reverse=True)

print(numbers)

Output:

[8, 5, 2, 1]

reverse()

Reverses the current order of the list.

Example:

fruits = ["Apple", "Banana", "Mango"]

fruits.reverse()

print(fruits)

Output:

['Mango', 'Banana', 'Apple']

copy()

Creates a copy of the list.

Example:

fruits = ["Apple", "Banana"]

new_list = fruits.copy()

print(new_list)

Output:

['Apple', 'Banana']

Practical Example

Example:

fruits = ["Apple", "Banana"]

fruits.append("Mango")
fruits.remove("Banana")
fruits.sort()

print(fruits)

Output:

['Apple', 'Mango']

Summary

  • append() adds items to the end.
  • insert() adds items at a specific position.
  • extend() combines lists.
  • remove() deletes an item by value.
  • pop() deletes an item by index.
  • clear() removes all items.
  • index() returns an item’s position.
  • count() counts occurrences.
  • sort() sorts items.
  • reverse() reverses the order.
  • copy() creates a duplicate list.