Skip to content

List Methods

PotatoScript edited this page Feb 27, 2025 · 2 revisions

📋 List Methods in Python 🐍

🔹 What Are List Methods? 🤔

List methods are built-in functions that allow you to perform operations on lists. They help you add, remove, sort, and modify items in your lists without needing to write complex code.


1️⃣ Adding Items to a List ➕

a. append(item)

  • Description: Adds an item to the end of the list.
  • Example:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")  # Adds 'orange' to the end
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange']

b. insert(index, item)

  • Description: Inserts an item at the specified index in the list.
  • Example:
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "kiwi")  # Inserts 'kiwi' at index 1
print(fruits)  # Output: ['apple', 'kiwi', 'banana', 'cherry']

2️⃣ Removing Items from a List ❌

a. remove(item)

  • Description: Removes the first occurrence of the specified item from the list.
  • Example:
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")  # Removes 'banana'
print(fruits)  # Output: ['apple', 'cherry']

b. pop(index)

  • Description: Removes and returns the item at the specified index. If no index is specified, it removes the last item.
  • Example:
fruits = ["apple", "banana", "cherry"]
last_fruit = fruits.pop()  # Removes the last item
print(last_fruit)  # Output: cherry
print(fruits)  # Output: ['apple', 'banana']

removed_fruit = fruits.pop(0)  # Removes the item at index 0
print(removed_fruit)  # Output: apple
print(fruits)  # Output: ['banana']

3️⃣ Modifying Items in a List ✏️

a. extend(iterable)

  • Description: Adds all elements from an iterable (like a list or tuple) to the end of the list.
  • Example:
fruits = ["apple", "banana"]
fruits.extend(["cherry", "orange"])  # Adds items from the list
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange']

b. sort()

  • Description: Sorts the items in the list in ascending order.
  • Example:
fruits = ["banana", "apple", "cherry"]
fruits.sort()  # Sorts the list
print(fruits)  # Output: ['apple', 'banana', 'cherry']

c. reverse()

  • Description: Reverses the order of the items in the list.
  • Example:
fruits = ["apple", "banana", "cherry"]
fruits.reverse()  # Reverses the list
print(fruits)  # Output: ['cherry', 'banana', 'apple']

4️⃣ Finding List Information 🔍

a. index(item)

  • Description: Returns the index of the first occurrence of the specified item.
  • Example:
fruits = ["apple", "banana", "cherry"]
index_of_banana = fruits.index("banana")
print(index_of_banana)  # Output: 1

b. count(item)

  • Description: Returns the number of times the specified item appears in the list.
  • Example:
fruits = ["apple", "banana", "cherry", "banana"]
count_of_banana = fruits.count("banana")
print(count_of_banana)  # Output: 2

c. clear()

  • Description: Removes all items from the list.
  • Example:
fruits = ["apple", "banana", "cherry"]
fruits.clear()  # Clears the list
print(fruits)  # Output: []

5️⃣ List Comprehension 🔄

While not a method, list comprehension is a powerful feature in Python that allows you to create new lists by applying an expression to each item in an existing list.

Example:

numbers = [1, 2, 3, 4]
squared_numbers = [x**2 for x in numbers]  # Squaring each number
print(squared_numbers)  # Output: [1, 4, 9, 16]

✅ Summary

  • List methods help you manage and manipulate lists efficiently.
  • You can add items using append() and insert(), remove items using remove() and pop(), and modify the list with extend(), sort(), and reverse().
  • You can find the index of an item with index(), count occurrences with count(), and clear the entire list with clear().
numbers = [1, 2, 3, 7, 5, 20]
numbers.append(30) # 30 will added to the end of the list
numbers.insert(0, 30) # 30 will be added in the beginner of the list, the first parameter is the index
numbers.insert(1, 30) # 30 will be added in the index 1 of the list
numbers.remove(5) # item with the value 5 will be removed from the list
numbers.clear() # will remove all the item in the list
numbers.pop() # this will remove the last item  in the list
numbers.index(3) # check the index of the value - output 2
numbers.count(5) # this will check the number of value in the list -> 1
numbers.sort() # this will sort the value in the List 
numbers.reverse() # will reverse your sort list value
numbers2 = numbers.copy() #  copy the list to another list
  • we also can check the existing of the value in the list by using in
numbers = [1,2,3,4,5,67,8]
print(50 in numbers) # output -> False

-Example

numbers = [2, 2, 4, 6, 3, 4, 6, 1]
uniques = []
for number in numbers:
    if number not in uniques:
       uniques.append(number)
print(uniques) # output -> 2,4,6,3,1

Clone this wiki locally