Python Arrays
Arrays are used to store multiple values in one single variable. The array values can be accessed through index of the stored variable.
Example:
input_array = ["apple", "banana", "strawberry"] print(input_array[0])
Output:
Apple
From the above example,
Functions
used with arrays:
Append -> this helps to append value at the end of an array.
input_array.append("mango") print(input_array)
Output:
['apple', 'banana', 'strawberry', 'mango']
Extend -> it help us to append more than one elements in array or append another array with the existing one.
input_array = ['apple', 'banana', 'strawberry', 'mango'] a = ["cranberry", "blueberry", "orange"] input_array.extend(a) print(input_array)
Output:
['apple', 'banana', 'strawberry', 'mango', 'cranberry', 'blueberry', 'orange']
Pop ->
this helps to pop the last element in the array
Example:
input_array = ['apple', 'banana', 'strawberry', 'mango'] input_array.pop()
Output:
‘mango’
Remove-> helps to remove the element from the array. If there
are duplicate elements in the array which you are trying to remove, it removes
the first occurring element in the array.
Example:
a = [1, 2, 3, 4, 4, 4, 5] a.remove(4) print(a)
Output:
[1, 2, 3, 4, 4, 5]
Similarly, we have many such functions in array operations such as