-
Notifications
You must be signed in to change notification settings - Fork 0
List Methods
PotatoScript edited this page Feb 27, 2025
·
2 revisions
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.
- 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']- 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']- 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']- 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']- 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']- 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']- 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']- 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- 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- Description: Removes all items from the list.
- Example:
fruits = ["apple", "banana", "cherry"]
fruits.clear() # Clears the list
print(fruits) # Output: []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.
numbers = [1, 2, 3, 4]
squared_numbers = [x**2 for x in numbers] # Squaring each number
print(squared_numbers) # Output: [1, 4, 9, 16]- List methods help you manage and manipulate lists efficiently.
- You can add items using
append()andinsert(), remove items usingremove()andpop(), and modify the list withextend(),sort(), andreverse(). - You can find the index of an item with
index(), count occurrences withcount(), and clear the entire list withclear().
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