This Python script (list_operations.ipynb) is designed to demonstrate the key operations and methods used to manipulate lists in Python, including append(), extend(), remove(), and del. The script includes comprehensive comments and examples to highlight the differences and appropriate use cases for each operation.
append(): Adds a single element to the end of the list.extend(): Extends the list by adding multiple elements from another iterable.remove(): Removes the first occurrence of a specified value from the list.del: Deletes elements by index or slices a portion of the list.
-
append()vsextend():append()adds a single element as a single entry.extend()adds multiple elements individually to the list.
-
remove()vsdel:remove()deletes the first occurrence of a specified value.deldeletes elements based on index positions or slices of elements.
The script also demonstrates how to handle common errors that occur when performing these operations:
-
ValueError:- Triggered when
remove()is called with a value that does not exist in the list. - Example:
my_list.remove("NonExistentItem")will raise aValueError.
- Triggered when
-
IndexError:- Triggered when
delis used with an out-of-range index. - Example:
del my_list[10]when the list has fewer than 10 items will raise anIndexError.
- Triggered when
##ZIRADDINGULUMJANLI2024