Understanding Lists and List Operations
A list is an ordered collection of elements, often used to store multiple values. To access or modify elements in a list, we use indexing. For example, to access the element at position i, we use the syntax aList[i]. The first element of a list is indexed as 1, so aList[1] refers to the first item.
Lists are dynamic structures, allowing programmers to add, remove, or modify elements. For instance, to assign a value of an element in the list to a variable, we use x = aList[i], where x takes the value of the element at index i. Similarly, assigning a value to an element in the list is done using aList[i] = x, which replaces the value at the specified index with x.
To insert an element at a specific index, we use the INSERT function: INSERT(aList, i, value). This function shifts all elements at indices greater than or equal to i to the right, making room for the new element. Conversely, the REMOVE function, REMOVE(aList, i), removes the item at index i and shifts the subsequent elements to the left.