Python Arrays
Python Arrays
Arrays
Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python
you will have to import a library, like the NumPy library.
Example
Create an array containing car names:
Try it Yourself »
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables
could look like this:
car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"
However, what if you want to loop through the cars and find a specific one? And what if you had
not 3 cars, but 300?
An array can hold many values under a single name, and you can access the values by referring to
an index number.
https://www.w3schools.com/python/python_arrays.asp 1/5
7/15/2021 Python Arrays
Example
Get the value of the first array item:
x = cars[0]
Try it Yourself »
Example
Modify the value of the first array item:
cars[0] = "Toyota"
Try it Yourself »
Example
Return the number of elements in the cars array:
x = len(cars)
Try it Yourself »
Note: The length of an array is always one more than the highest array index.
https://www.w3schools.com/python/python_arrays.asp 2/5
7/15/2021 Python Arrays
Example
Print each item in the cars array:
for x in cars:
print(x)
Try it Yourself »
Example
Add one more element to the cars array:
cars.append("Honda")
Try it Yourself »
Example
Delete the second element of the cars array:
cars.pop(1)
https://www.w3schools.com/python/python_arrays.asp 3/5
7/15/2021 Python Arrays
Try it Yourself »
You can also use the remove() method to remove an element from the array.
Example
Delete the element that has the value "Volvo":
cars.remove("Volvo")
Try it Yourself »
Note: The list's remove() method only removes the first occurrence of the specified value.
Array Methods
Python has a set of built-in methods that you can use on lists/arrays.
Method Description
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
https://www.w3schools.com/python/python_arrays.asp 4/5