A List can be considered as a dynamic array. It is denoted by [ ]. The values inside a list are called elements. The values in a list can be homogeneous (i.e., all elements are of the same data type) or they can be heterogeneous (i.e., a mix of multiple data types).
# An empty list
list_1 = [ ]
# A homogeneous list (all elements are of type integers)
list_2 = [1, 2, 3, 4, 5]
# A heterogeneous list (mix of different types)
list_3 = [1, 2.5, "language", True, ['a', 'b', 'c', 'd', 'e']]
A list is dynamic because the size of the array automatically changes with the addition or deletion of list elements.
When an empty list is created, Python allocates 0 slots, but the moment you add your first element, it internally provisions 4 spaces in memory. Subsequently, if those 4 spaces are filled, it expands the size of the list to 8 spaces, creating 4 additional slots. Specific calculations exist in Python's core logic behind this step-by-step expansion.
Lists are ordered, meaning the original order of insertion is maintained. If a new element is appended, it is automatically placed at the last position in the list. Lists also accept **duplicate **elements.
The operations that can be performed on a list can be obtained by running dir(list_name).
The operations are performed using dot notation:
e.g., To add an element, use: list_name.append("New Value")
Accessing Elements via Indexing:
The elements in a list can be accessed using their index value. The index starts from 0 and extends to n-1 for a list of n elements.
list_retrieval = [4, 3, 6, 7, 9, 1]
Here:
list_retrieval[0] will return 4
list_retrieval[3] will return 7
list_retrieval[9] will throw an IndexError: list index out of range because index 9 does not exist.
Similarly, elements can be accessed in reverse order. The last element is indicated by -1, and the first element is -n. Since our list has 6 elements (n=6), the first element can also be accessed at -6.
Retrieving Subsets via Slicing:
The elements in a list can be retrieved in bulks using a 'slicing' operation: [start_index : end_index : step]
Using our list_retrieval = [4, 3, 6, 7, 9, 1] example:
Values from the beginning up to the 4th element can be retrieved as list_retrieval[:4] or list_retrieval[0:4]. The returned list is [4, 3, 6, 7].
Values from the 3rd index to the end can be accessed as list_retrieval[3:]. The returned list is [7, 9, 1].
Values from the 2nd index to the 5th index (exclusive) can be accessed as list_retrieval[2:5]. The returned list is [6, 7, 9].
Note: The starting index is inclusive and the ending index is exclusive. Meaning in [0:4], the 0th index is included, but the 4th index is excluded from the final result.
The step parameter controls the increment between elements. By default, the step is 1, meaning it reads every consecutive element.
If you set the step to 2, it skips every other element.
Using our list_retrieval = [4, 3, 6, 7, 9, 1] example:
list_retrieval[0:6:2]will return[4, 6, 9](it starts at index 0 and takes every second element).list_retrieval[::-1]is a famous Python trick! By omitting the start/end indexes and using a negative step of-1, it returns the entire list completely reversed:[1, 9, 7, 6, 3, 4].
Top comments (0)