List Methods: Append, Extend, Count, Remove, Index, Pop, Insert, Sort, Reverse

Python provides several built in methods to manipulate and manage lists efficiently.

1. append()

The append() method adds a single element at the end of the list.

Syntax

list.append(element)

Example

fruits = ["Apple", "Mango"]

fruits.append("Orange")

print(fruits)

Output

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

Characteristics

  1. Adds only one element at a time.
  2. Modifies the original list.
  3. Element is added at the end.

2. extend()

The extend() method adds multiple elements from another iterable to the end of the list.

Syntax

list.extend(iterable)

Example

fruits = ["Apple", "Mango"]

fruits.extend(["Orange", "Banana"])

print(fruits)

Output

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

Characteristics

  1. Adds multiple elements.
  2. Extends the existing list.
  3. Accepts lists, tuples, strings, etc.

3. count()

The count() method returns the number of occurrences of a specified element.

Syntax

list.count(element)

Example

numbers = [10, 20, 10, 30, 10]

print(numbers.count(10))

Output

3

Uses

  1. Counting repeated values.
  2. Frequency analysis.
  3. Data validation.

4. remove()

The remove() method removes the first occurrence of a specified element.

Syntax

list.remove(element)

Example

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

fruits.remove("Mango")

print(fruits)

Output

['Apple', 'Orange']

Characteristics

  1. Removes only the first matching element.
  2. Generates an error if the element is not found.

5. index()

The index() method returns the position of the first occurrence of an element.

Syntax

list.index(element)

Example

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

print(fruits.index("Mango"))

Output

1

Characteristics

  1. Returns the first matching index.
  2. Generates an error if the element is absent.

6. pop()

The pop() method removes and returns an element from a specified position.

Syntax

list.pop(index)

Example 1

numbers = [10, 20, 30, 40]

numbers.pop()

print(numbers)

Output

[10, 20, 30]

Example 2

numbers = [10, 20, 30, 40]

numbers.pop(1)

print(numbers)

Output

[10, 30, 40]

Characteristics

  1. Removes and returns the element.
  2. By default removes the last element.

7. insert()

The insert() method inserts an element at a specified position.

Syntax

list.insert(index, element)

Example

fruits = ["Apple", "Orange"]

fruits.insert(1, "Mango")

print(fruits)

Output

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

Characteristics

  1. Inserts at a given index.
  2. Existing elements shift to the right.

8. sort()

The sort() method arranges list elements in ascending order by default.

Syntax

list.sort()

Example

numbers = [40, 10, 30, 20]

numbers.sort()

print(numbers)

Output

[10, 20, 30, 40]

Descending Order

numbers = [40, 10, 30, 20]

numbers.sort(reverse=True)

print(numbers)

Output

[40, 30, 20, 10]

Characteristics

  1. Modifies the original list.
  2. Sorts numbers and strings.
  3. Supports ascending and descending order.

9. reverse()

The reverse() method reverses the order of elements in a list.

Syntax

list.reverse()

Example

numbers = [10, 20, 30, 40]

numbers.reverse()

print(numbers)

Output

[40, 30, 20, 10]

Characteristics

  1. Reverses the list order.
  2. Changes the original list.
  3. Different from sorting in descending order.

Summary Table

Method Purpose
append() Adds one element at the end
extend() Adds multiple elements
count() Counts occurrences of an element
remove() Removes first occurrence
index() Returns position of element
pop() Removes and returns element
insert() Inserts element at specified position
sort() Sorts list elements
reverse() Reverses list order

Difference Between append() and extend()

Basis append() extend()
Elements Added One element Multiple elements
Argument Single object Iterable
Result Adds object as one item Adds each item separately

Example

list1 = [1, 2]

list1.append([3, 4])
print(list1)

list2 = [1, 2]

list2.extend([3, 4])
print(list2)
Output
[1, 2, [3, 4]]
[1, 2, 3, 4]

Difference Between remove() and pop()

Basis remove() pop()
Removes By Value Index
Return Value No Yes
Default Action No default Removes last element

Leave a Reply

error: Content is protected !!