Python Arrays
Python Arrays
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
Python HOME
Python Intro ADVERTISEMENT
Python Get Started
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Numbers
Python Arrays
Python Casting
❮ Previous Next ❯
Python Strings
Python Booleans
Python Operators
Python Lists
Python Tuples Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
Python Sets
Python Dictionaries
Python If...Else
Python While Loops
Python For Loops
Arrays
Python Functions
Python Lambda
Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a
Python Arrays
library, like the NumPy library.
Python Classes/Objects
Python Inheritance
Python Iterators
Arrays are used to store multiple values in one single variable:
Python Polymorphism
Python Scope
Try it Yourself »
What is an Array?
COLOR PICKER
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.
Example
Get the value of the first array item:
x = cars[0]
Try it Yourself »
Example
Modify the value of the first array item:
ADVERTISEMENT
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.
ADVERTISEMENT
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)
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
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
ADVERTISEMENT
ADVERTISEMENT
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.