Python Arrays
Note : The Python support for Arrays is not built-in, but alternatively Python lists can be utilised.
What is an Array?
A array is a particular variable, which has several values at a time.
If you have a list of items (for example a list of car names), it may look like that to store names in one variables :
car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"
But what if you want to loop around the names and find a particular car? What if you didn't have 3 names, but 300 ?
An array can have several values under one name, with a number of indexes. You can access these values.
Example :- Create an array containing car names :
cars = ["Ford", "BMW", "Toyoto"]
print(names)
Output :-
Note: You can utilise LISTS as ARRAYS but you will have to include a library, such as the NumPy library, for dealing with arrays in Python.
Access the Elements of an Array
In the index number, you refer to an array element.
Example 1 :- Get the value of the first array item :
names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]
x = names[0]
print(x)
Output :-
Example 2 :- Modify the value of the second array item :
names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]
names[1] = "Google"
print(names)
Output :-
The Length of an Array
Use len()
function is used to return the array size (the number of elements in an array).
Example :- Return the number of items in the array of names
:
names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]
x = len(names)
print(x)
Output :-
Note : The array length is always the highest array index.
Looping Array Elements
You can use the for in
loop statement to loop all array elements.
Example :- Print each item in the names
array :
names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]
for x in names:
print(x)
Output :-
Apple
Yahoo
Wikipedia
Related Links
Adding Array Elements
To add an element to the array you can use the append()
method.
Example :- Add one more element to the names
array :
names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]
names.append("Google")
print(names)
Output :-
Removing Array Elements
To delete an item from the array, use the pop()
method.
Example 1 :- Delete the second element of the names
array :
names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]
names.pop(1)
print(names)
Output :-
You may also remove an item in the array by using the remove()
method.
Example 2 :- Remove the value "Yahoo" element :
names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]
names.remove("Yahoo")
print(names)
Output :-
Note : Only the first occurrence of that value can be removed by the list remove()
method.
Related Links
Array Methods
Python includes a collection of integrated methods that can be used on lists/arrays.
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Note : The Arrays are not supported by Python but alternatively Python Lists can be utilised.