You may ask how to use Python arrays. Well, Python does not provide a data structure with the name of the array but instead of arrays, it provides the List data type. The Python list provides the same functionality and usage with the array in other programming languages like C#, C, PHP, etc. In this tutorial, we will use the terms array and list interchangeably where simply show how to use lists like arrays.
The Python array can be created in different ways but the simple and fast way to create an array is using the brackets and providing the items by separating them with comma. In the following example we create an array named
names and put some names as array items.
names = [ "ismail" , "ahmet" , "elif" ]
Alternatively an array can be created by using the
list() function like below where items are provided as parameter to the list() function.
names = list(("ismail" , "ahmet" , "elif"))
Acces Array Items
Python array provides indexing for each item. The items can be accessed by specifying the item index in square bracket. In the following example we access the items located at index numbers 1 and 2.
names = [ "ismail" , "ahmet" , "elif" ] a = names b = names
Modify Item in Array
The Python array can be modified by accessing them using item index numbers. Just provide the item index number in square brackets and set the new value for the existing item place.
names = [ "ismail" , "ahmet" , "elif" ] names = "ali"
Length of Array
The array item count or length of the array can be found by using the
len() method. The array variable is provided as parameter to the len() method like below.
names = [ "ismail" , "ahmet" , "elif" ] count = len(names)
Iterate over Array
Python array is an iterable data structure which makes it very powerfull and usefull. The items of the array can be iterated in different ways by using different loop types like
for loop ,
while loop etc.
names = [ "ismail" , "ahmet" , "elif" ] for item in names: print(item)
ismail ahmet elif
Add Item to Array
New items can be added into the existing array in Python. The
add() function of the array object can be used to add new item. The new item is provided as parameter to the add() method.
names = [ "ismail" , "ahmet" , "elif" ] names.add("ali")
Remove Item from Array
Existing item can be removed from the Python array by using the
remove() method. The item value we want to remove shoudl be provided to the remove() method.
names = [ "ismail" , "ahmet" , "elif" ] names.remove("ismail")
del statement can be used to remove the specified item from the Python array. The item can be specified by using its index number.
names = [ "ismail" , "ahmet" , "elif" ] del names
Sort Array Items
The Python array provides the
sort() method in order to sort items in the specified array. The items in the array is sorted inplace.
names = [ "ismail" , "ahmet" , "elif" ] names.sort()
Python array provides a lot of useful methods which can be used to add, remove, edit, pop, count the items or array.
|append()||Add a new item to the array|
|clear()||Remove and clear all items in an array|
|copy()||Copy and clone the array|
|count()||Count array items|
|index()||Search, find and return the index of the specified value in the array|
|insert()||Add a new item into the specified index in an array|
|pop()||Remove the element at the specified position or index of the array|
|remove()||Remove the first item with the specified value from the array|
|sort()||Sort the list items|