There are several built-in methods in Python List, these methods can make you use list more efficiently.
Method | Description |
append() | Adds an item to the end of the list |
extend() | Adds items of lists and other iterables to the end of the list |
insert() | Inserts an item at the specified index |
remove() | Removes the specified value from the list |
pop() | Returns and removes item present at the given index |
clear() | Removes all items from the list |
index() | Returns the index of the first matched item |
count() | Returns the count of the specified item in the list |
sort() | Sorts the list in ascending/descending order |
reverse() | Reverses the item of the list |
copy() | Returns the shallow copy of the list |
append()
Python List append()
adds a single item to the end of the list.
Syntax
my_list.append(new_element)
Parameters
new_element
: The element that you want to add to the end of the list
Example:
# Create a list my_list = [1, 2, 3] # Add a new element to the end of the list my_list.append(4) # Print the updated list print(my_list) # Output: [1, 2, 3, 4]
extend()
Python List extend()
is used to extend a list by appending elements from another iterable (such as another list, tuple, or any iterable object).
Syntax
my_list.extend(iterable)
Parameters
iterable
: An iterable object (e.g., list, tuple, set) containing elements that you want to add to the end of the list.
Example:
# Create two lists list1 = [1, 2, 3] list2 = [4, 5, 6] # Extend list1 by adding elements from list2 list1.extend(list2) # Print the updated list1 print(list1) # Output: [1, 2, 3, 4, 5, 6]
Python List Add (+) and Multiply (*) Operation – Using +
and *
operation to extend a list.
insert()
Python List insert()
will insert an element at a specified position in a list.
Syntax
my_list.insert(index, element)
Parameters
index
: The index at which you want to insert the new element.element
: The element you want to insert into the list.
Example:
# Create a list my_list = [1, 2, 3, 4] # Insert an element at index 2 my_list.insert(2, 10) # Print the updated list print(my_list) # Output: [1, 2, 10, 3, 4]
remove()
Python List remove()
will remove the first occurrence of a specified value from a list. If the value appears multiple times in the list, only the first occurrence will be removed.
Syntax
my_list.remove(value)
Parameters
value
: The element you want to remove from the list.
Example:
# Create a list my_list = [10, 20, 30, 20, 40] # Remove the value 20 from the list my_list.remove(20) # Print the updated list print(my_list) # Output: [10, 30, 20, 40
pop()
Python List pop()
is used to remove and return an element from a specific position in a list.
Syntax
popped_element = my_list.pop(index)
Parameters
index (optional)
: The index of the element you want to remove from the list. If not provided, pop() removes and returns the last element.
Return Value
popped_element
: the element removed from the list
Example
# Create a list my_list = [10, 20, 30, 40] # Remove and retrieve the element at index 1 popped_element = my_list.pop(1) # Print the popped element and the updated list print("Popped element:", popped_element) # Output: 20 print("Updated list:", my_list) # Output: [10, 30, 40]
clear()
Python List clear()
will remove all elements from a list.
Syntax
my_list.clear()
Example
# Create a list my_list = [1, 2, 3, 4, 5] # Clear the list my_list.clear() # Print the empty list print(my_list) # Output: []
index()
Python List index()
finds the index of the first occurrence of a specified element in a list. It returns the index of the element if it is found in the list, and if the element is not found, a ValueError
is raised.
Syntax
index = my_list.index(element, start, end)
Parameters
element
: The element you want to find the index of in the list.start (optional)
: The index in the list from which the search begins. If not specified, the search starts from the beginning of the list.end (optional)
: The index in the list where the search ends. If not specified, the search goes until the end of the list.
Return Value
Index
: the index of the first occurrence of the specified element in the list.
Example:
# Create a list my_list = [10, 20, 30, 20, 40] # Find the index of the element 20 index = my_list.index(20) # Print the index print(index) # Output: 1
count()
The count()
method is used to count the number of occurrences of a specified element in a list.
Syntax:
count = my_list.count(element)
Parameters
element
: The element for which you want to count occurrences in the list.
Return Value
count
: the number of times the specified element appears in the list.
Exampe:
# Create a list my_list = [10, 20, 30, 20, 40, 20] # Count the number of occurrences of the element 20 count = my_list.count(20) # Print the count print(count) # Output: 3
sort()
Python List sort()
will sort the elements of a list in place. By default, the sort() method arranges the elements in ascending order.
Syntax:
my_list.sort(key=None, reverse=False)
Parameters
key (optional)
: A function that serves as a key for the sort comparison. If provided, elements are sorted based on this key.reverse (optional)
: A boolean indicating whether to sort the list in descending order (True) or not (False). By default, it is set to False.
Example:
# Create a list my_list = [5, 2, 8, 1, 3] # Sort the list in ascending order my_list.sort() # Print the sorted list print(my_list) # Output: [1, 2, 3, 5, 8]
reverse()
The reverse()
method is used to reverse the order of elements in a list in place.
Syntax
my_list.reverse()
Example:
# Create a list my_list = [1, 2, 3, 4, 5] # Reverse the order of elements in the list my_list.reverse() # Print the reversed list print(my_list) # Output: [5, 4, 3, 2, 1]
copy()
Python List copy()
method for lists is used to create a shallow copy of the list.
Syntax
new_list = my_list.copy()
Example:
# Create a list my_list = [1, 2, 3, 4, 5] # Create a copy of the list new_list = my_list.copy() # Modify the new list new_list[0] = 10 # Print both lists print(my_list) # Output: [1, 2, 3, 4, 5] print(new_list) # Output: [10, 2, 3, 4, 5]