In python, arrays are useful for storing multiple values of the same data type, but we don’t have direct built-in support for arrays in python. To work with arrays in python, you need to import the NumPy
library or use lists in python to achieve the array's functionality.
This article will show you how to use python lists as arrays to store the multiple values in a single variable with examples.
In python, you can create the array by enclosing the items within square brackets [ ]
and the items must be separated by commas.
Following is the example of creating the arrays in python.
If you observe the above example, we created different arrays and printing the array items using the print method in python.
When you execute the above python program, you will get the result as shown below.
In python, you can access elements from an array by using index values. In array, the index values will always start from 0
. If your array has 4
elements, you can access the array elements by using index ranges 0
to 3
.
Following is the example of accessing the elements from an array in python.
If you observe the above example, we are accessing the array elements using different index values with square brackets [ ]
.
When you execute the above python example, you will get the result as shown below.
Using index values, you can change or update the value of the required item in the array.
Following is the example of changing the particular array item value using index number in python.
When you execute the above python array example, you will get the result as shown below.
In python, the append()
method is useful to add or append items at the end of the array.
Following is the example of adding items to the array using the append()
method in python.
The above python array example will return the result as shown below.
In python, you can delete/remove items from an array using either. remove()
, pop()
, or clear()
methods based on our requirements.
In python, if you want to remove array items based on the value, you can use the remove()
method.
Following is the example of removing the array items based on the value using the remove()
method.
The above python array example will return the result as shown below.
In python, the pop()
method helps remove the array items based on the specified index position. In case if index position is not specified, by default, it will remove the last item.
Following is the example of using the pop()
method to remove array items.
When you execute the above python array program, you will get the result as shown below.
If you want to clear or empty array items, you can use the clear()
method.
Following is the example of removing all array elements using the clear()
method in python.
The above python array example will return the result as shown below.
In python, to count the number of items in the array, use the len()
function.
Following is the example of using the len()
function in python to get the array length/size.
By using len()
function, you can also check whether the array is empty or not like as shown below.
The above array example will return the result as shown below.
In python, the +
operator is useful to join or concatenate multiple arrays and return a new array with all array elements.
Following is the example of joining the arrays using the +
operator in python.
The above array example will return the result as shown below.
By using in
and not in
operators, you can check whether the particular item exists in the array or not.
Following is the example to verify whether the particular exists in the array or not using in
operator in python.
The above array example will return the result as shown below.
By using for loop, you can loop through the elements of the array based on your requirements.
Following is the example of looping through the array items using for loop in python.
The above array example will return the result as shown below.
In python, the sort()
method is useful to sort the array elements. By default, the sort()
method will sort the array elements in ascending order.
Following is the example of sorting the array elements in ascending order using the sort()
method in python.
The above array example will return the result as shown below.
To sort the array elements in descending order, you need to use the optional reverse = True
property with the sort()
method as shown below.
The above array example will return the result as shown below.
To sort array items either in ascending or descending order, you need to make sure that all the array items must be the same type otherwise, you will get a TypeError
exception.
When you execute the above python array example, you will get the result as shown below.
In python, the list/array object has a set of built-in methods to perform different operations based on your requirements.
Following are the built-in methods that you can use on lists/arrays in python.
Method | Description |
---|---|
append() | It is useful to add elements at the end of the list. |
insert() | It is useful to add elements at the specified position. |
extend() | It is useful to add elements of one list to the end of another list. |
pop() | It is useful to remove the element at the specified position. |
remove() | It is useful to remove the element with the specified value. |
clear() | It is useful to remove all the elements of the list. |
index() | It will return the index of the first matched element. |
count() | It will return the number of elements with the specified value. |
sort() | It is useful to sort (ascending/descending) list items. |
reverse() | It is useful to reverse the order of list items. |
copy() | It is useful to return a copy of the list. |