Append is a predefined function. The append() function is used to add a new element to the end of an existing list.
The syntax for the append() function is listobj. append(Value), where Value is the element to be added.
# Creating a list
lst = [10, 20, -30, 40]
# Printing the original list
print("Original list:", lst) # Output: Original list: [10, 20, -30, 40]
# Appending elements using append()
lst.append(50) # Adding integer 50 to the end
lst.append("Python") # Adding string "Python" to the end
# Printing updated list
print("List after appending:", lst) # Output: List after appending: [10, 20, -30, 40, 50, 'Python']
# Appending a mixed-type element
lst.append(3.14) # Adding float 3.14 to the end
# Printing final list
print("Final list:", lst) # Output: Final list: [10, 20, -30, 40, 50, 'Python', 3.14]
Insert is a predefined function. The insert() function is used to add an element at a specified position or index in a list.
The syntax for the insert() function is listobj.insert(Index, Value), where Index is the position and Value is the element to be added.
# Creating a list with variable name 'lst'
lst = [10, 20, -30, 40]
# Printing original list
print("Original list:", lst) # Output: Original list: [10, 20, -30, 40]
# Inserting elements using insert()
lst.insert(1, 15) # Adding integer 15 at index 1
lst.insert(0, "Start") # Adding string "Start" at index 0
# Printing updated list
print("List after inserting:", lst) # Output: List after inserting: ['Start', 10, 15, 20, -30, 40]
# Inserting a mixed-type element
lst.insert(3, 3.14) # Adding float 3.14 at index 3
# Printing final list
print("Final list:", lst) # Output: Final list: ['Start', 10, 15, 3.14, 20, -30, 40]
The extend() function is a predefined function used to add multiple elements at the end of a list.
The syntax for the extend() function is listobj. extend(list), where Index is thes the element to be added.
# Creating a list with variable name 'lst'
lst = [10, 20, -30, 40]
# Printing original list
print("Original list:", lst) # Output: Original list: [10, 20, -30, 40]
# Extending the list with another list
lst.extend([50, 60]) # Adding elements [50, 60] at the end
print("List after extending with [50, 60]:", lst) # Output: List after extending with [50, 60]: [10, 20, -30, 40, 50, 60]
# Extending the list with a tuple
lst.extend((3.14, "Python")) # Adding elements 3.14 and "Python" at the end
print("List after extending with (3.14, 'Python'):", lst) # Output: List after extending with (3.14, 'Python'): [10, 20, -30, 40, 50, 60, 3.14, 'Python']
# Extending the list with a string (treated as iterable of characters)
lst.extend("ABC") # Adding characters 'A', 'B', 'C' at the end
print("List after extending with 'ABC':", lst) # Output: List after extending with 'ABC': [10, 20, -30, 40, 50, 60, 3.14, 'Python', 'A', 'B', 'C']
The remove() function is used to delete the first occurrence of a specified element from a list based on its value.
The syntax is listobj. remove(Value), where Value is the element to be removed.
# Creating a list with variable name 'lst'
lst = [10, 20, -30, 20, 40, "Python"]
# Printing original list
print("Original list:", lst) # Output: Original list: [10, 20, -30, 20, 40, 'Python']
# Removing the first occurrence of an element
lst.remove(20) # Removes the first occurrence of 20
print("List after removing 20:", lst) # Output: List after removing 20: [10, -30, 20, 40, 'Python']
# Removing another element
lst.remove("Python") # Removes the first occurrence of "Python"
print("List after removing 'Python':", lst) # Output: List after removing 'Python': [10,
If the specified element is not present in the list, a ValueError is raised.
# Creating a list with variable name 'lst'
lst = [10, 20, -30, 20, 40, "Python"]
# Printing original list
print("Original list:", lst) # Output: Original list: [10, 20, -30, 20, 40, 'Python']
lst.remove(50) # 50 is not in the list, will raise ValueError
The pop() function is used to remove and return an element from a list at a specified valid index; if the index is invalid, it raises an IndexError.
The syntax is listobj.pop(index), where index is the position of the element to be removed.
# Creating a list with variable name 'lst' and mixed data types
lst = [10, "Start", 3.14, True, -30, "Python"]
# Printing original list
print("Original list:", lst) # Output: Original list: [10, 'Start', 3.14, True, -30, 'Python']
# Popping an element at a specific index
popped_item = lst.pop(1) # Removes and returns element at index 1 ('Start')
print("List after popping index 1:", lst) # Output: List after popping index 1: [10, 3.14, True, -30, 'Python']
print("Popped item:", popped_item) # Output: Popped item: Start
# Popping the last element (default behavior, no index specified)
popped_item = lst.pop() # Removes and returns the last element ('Python')
print("List after popping last element:", lst) # Output: List after popping last element: [10, 3.14, True, -30]
print("Popped item:", popped_item) # Output: Popped item: Python
If no index is provided, it removes and returns the last element by default.
# Creating a list with variable name 'lst' and mixed data types
lst = [10, "Start", 3.14, True, -30, "Python"]
# Printing original list
print("Original list:", lst) # Output: Original list: [10, 'Start', 3.14, True, -30, 'Python']
lst.pop(7) # Index 7 does not exist, will raise IndexError
The pop() function, when called without an index, is used to remove and return the last element of a list.
The syntax is listobj.pop(), which deletes the last element from the list.
# Creating a list with variable name 'lst' and mixed data types
lst = [10, "Start", 3.14, True, -30]
# Printing original list
print("Original list:", lst) # Output: Original list: [10, 'Start', 3.14, True, -30]
# Popping the last element
popped_item = lst.pop() # Removes and returns the last element (-30)
print("List after popping last element:", lst) # Output: List after popping last element: [10, 'Start', 3.14, True]
print("Popped item:", popped_item) # Output: Popped item: -30
# Popping the last element again
popped_item = lst.pop() # Removes and returns the last element (True)
print("List after popping last element:", lst) # Output: List after popping last element: [10, 'Start', 3.14]
print("Popped item:", popped_item) # Output: Popped item: True
If the list is empty, it raises an IndexError.
# Creating an empty list to demonstrate error case
lst_empty = []
# Attempting to pop from an empty list
lst_empty.pop() # Will raise IndexError since the list is empty
The clear function is used to remove all elements from a list, leaving it empty.
The syntax is listobj.clear(), which deletes all elements from the list without returning any value.
# Creating a list with variable name 'lst' and mixed data types
lst = [10, "Start", 3.14, True, -30]
# Printing the original list
print("Original list:", lst) # Output: Original list: [10, 'Start', 3.14, True, -30]
print("Length of original list:", len(lst)) # Output: Length of original list: 5
# Using clear() to remove all elements
lst.clear() # Removes all elements, making the list empty
print("List after clear():", lst) # Output: List after clear(): []
print("Length after clear():", len(lst)) # Output: Length after clear(): 0
The del statement is a general-purpose function used to remove elements from any mutable object (like a list) based on indexing or slicing, or to delete the entire object.
The del statement can target a single element (del listobj[index]), a range of elements (del listobj[start :end]), or the entire object (del listobj), modifying the mutable object in place or removing it from memory.
# Creating a list with variable name 'lst' and mixed data types
lst = [10, "Start", 3.14, True, -30, "Python"]
# Printing the original list
print("Original list:", lst) # Output: Original list: [10, 'Start', 3.14, True, -30, 'Python']
print("Length of original list:", len(lst)) # Output: Length of original list: 6
# Using del to remove a single element by index
del lst[1] # Removes element at index 1 ('Start')
print("List after del lst[1]:", lst) # Output: List after del lst[1]: [10, 3.14, True, -30, 'Python']
print("Length after del:", len(lst))
# Creating a list with variable name 'lst' and mixed data types
lst = [10, "Start", 3.14, True, -30, "Python"]
# Printing the original list
print("Original list:", lst) # Output: Original list: [10, 'Start', 3.14, True, -30, 'Python']
print("Length of original list:", len(lst)) # Output: Length of original list: 6
# Using del to remove a range of elements by slicing
del lst[1:4] # Removes elements from index 1 to 3 (3.14, True, -30)
print("List after del lst[1:4]:", lst) # Output: List after del lst[1:4]: [10, 'Python']
print("Length after del:", len(lst)) # Output: Length after del: 2
# Creating a list with variable name 'lst' and mixed data types
lst = [10, "Start", 3.14, True, -30, "Python"]
del lst # Deletes the entire list object
print(last) # it shows error
lst = [1, 2, 3]
del lst[10] # Index 10 does not exist, will raise IndexError
This function is used to count the number of occurrences of a specified element in a list.
Syntax: listobj. count(value).
It returns the number of times the specified value appears in the list.
# Counting occurrences of a number in a list
numbers = [1, 2, 3, 2, 4, 2, 5]
count_of_twos = numbers.count(2)
print(f"Number of 2s in the list: {count_of_twos}") # Output: Number of 2s in the list: 3
# Counting occurrences of a string in a list
fruits = ["apple", "banana", "apple", "orange", "apple"]
count_of_apples = fruits.count("apple")
print(f"Number of 'apple' in the list: {count_of_apples}") # Output: Number of 'apple' in the list: 3
Counting an element not present in the list
count_of_mango = fruits.count("mango")
print(f"Number of 'mango' in the list: {count_of_mango}") # Output: Number of 'mango' in the list: 0
This function is used to find the index of the first occurrence of a specified element in a list.
If the specified element is not found in the list, it raises a ValueError.
Syntax: listobj.index(element)
# Finding the index of a float in a list
prices = [19.99, 29.99, 15.50, 29.99, 45.00]
index_of_price = prices.index(29.99)
print(f"Index of 29.99: {index_of_price}") # Output: Index of 29.99: 1
# Finding the index of a character in a list of characters
letters = ['a', 'b', 'c', 'a', 'd']
index_of_c = letters.index('c')
print(f"Index of 'c': {index_of_c}") # Output: Index of 'c': 2
# Using index() with a mixed-type list
mixed_list = [1, "hello", 3.14, "hello", True]
index_of_false = mixed_list.index(False) # Output: Error: 'False' not found in the list