Array and List in Python
Array and List in Python
Array and List in Python
Array is very similar to a list function in Python. However, the two has differences. First, an array
has a limited number of operations, which commonly include those for array creation, reading a
value from a specific element, and writing a value to a specific element. While the list, on the other
hand, provides a large number of operations for working with the contents of the list. Second, the
list can grow and shrink during execution as elements are added or removed while the size of an
array cannot be changed after it has been created.
pyList = [4,12,2,34,17]
print(len(pyList))
The list() constructor is being called to create a list object with values. When the list()
constructor is called, an array structure is created to store the items contained in the list. The
array is initially created bigger than needed, leaving capacity for future expansion. The values
stored in the list comprise a subarray in which only a contiguous subset of the array elements
are actually used. The len() function can be used to return the length of the list.
pyList.append(50)
If there is room in the array, the item is stored in the next available slot of thearray and the
length field is incremented by one.
pyList.append(64)
pyList.append(6)
Unlike in array that cannot change the size once it has been create, when third statement
is executed the list will have to be expanded to make room for value 6. This makes list contain
any number of items and never becomes full.
A list can be appended to a second list using the extend()method as shown inthe following
example:
pyListA = [34,12]
pyListB = [4,6,31,9]
print(pyListA)
pyListA.extend(pyListB)
print(pyListA)
If the list being extended has the capacity to store all of the elements from the second list,
the elements are simply copied, element by element. If there is capacity for all of the elements,
the underlying array has to be expanded as was done with the append() method. Since Python
knows how big the array needs to be in order to store all of the elements from both lists, it only
requires a single expansion of the destination list, pyListA. The new array will be created larger
than needed to allow more items to be added to the list without first requiring an immediate
expansion of the array. After the new array is created, elements from the destination list are copied
to the new array followed by the elements from the source list, pyListB.
pyList.insert(3,79)
The above code will insert value 79 at index position 3. Since there is already item at that
position, we must make room for the new item by shifting all of the items down one position
starting with the item at index position 3. After shifting the items, the value 79 is then inserted at
position 3.
Removing Items
The pop() method removes the specified index, (or the last item if index is not specified):
The first statement removes the first item from the list. After the item is removed, typically
by setting the reference variable to None, the items following it within the array are shifted down,
from left to right, to close the gap. Finally, the length of the list is decremented to reflect the smaller
size.