Python Array contains a sequence of data. In python programming, there is no exclusive array object because we can perform all the array operations using list. Today we will learn about python array and different operations we can perform on an array (list) in python. I will assume that you have the basic idea of python variables and python data types.
Python Array
Python supports all the array related operations through its list object. Let’s start with one-dimensional array initialization.
Python array example
Python array elements are defined within the brace []
and they are comma separated. The following is an example of declaring python one-dimensional array.
1 2 3 4 5 6 |
arr = [ 1, 2 ,3, 4, 5] print (arr) print (arr[2]) print (arr[4]) |
Output of above one dimensional array example program will be:
1 2 3 4 5 |
<span style="color: #008000;"><strong><code> [1, 2, 3, 4, 5] 3 5 </code></strong></span> |
Array indexing starts from 0. So the value of index 2 of variable arr is 3.
In some other programming languages such as Java, when we define an array we also need to define element type, so we are limited to store only that type of data in the array. For example, int brr[5];
is able to store integer data only.
But python gives us the flexibility to have the different type of data in the same array. It’s cool, right? Let’s see an example.
1 2 3 4 5 |
student_marks = ['Akkas' , 45, 36.5] marks = student_marks[1]+student_marks[2] print(student_marks[0] + ' has got in total = %d + %f = %f ' % (student_marks[1], student_marks[2], marks )) |
It give the following output:
1 2 3 |
Akkas has got in total = 45 + 36.500000 = 81.500000 marks |
In the above example you can see that, student_marks
array have three type of data – string, int and float.
Python multidimensional array
Two dimensional array in python can be declared as follows.
1 2 3 4 5 6 |
arr2d = [ [1,3,5] ,[2,4,6] ] print(arr2d[0]) # prints elements of row 0 print(arr2d[1]) # prints elements of row 1 print(arr2d[1][1]) # prints element of row = 1, column = 1 |
It will produce the following output:
1 2 3 4 5 |
[1, 3, 5] [2, 4, 6] 4 |
Similarly, we can define a three-dimensional array or multidimensional array in python.
Python array examples
Now that we know how to define and initialize an array in python. We will look into different operations we can perform on a python array.
Python array traversing using for loop
We can use for loop to traverse through elements of an array. Below is a simple example of for loop to traverse through an array.
1 2 3 4 5 |
arrayElement = ["One", 2, 'Three' ] for i in range(len(arrayElement)): print(arrayElement[i]) |
Below image shows the output produced by the above array example program.
Traversing 2D-array using for loop
The following code print the elements row-wise then the next part prints each element of the given array.
1 2 3 4 5 6 7 8 |
arrayElement2D = [ ["Four", 5, 'Six' ] , [ 'Good', 'Food' , 'Wood'] ] for i in range(len(arrayElement2D)): print(arrayElement2D[i]) for i in range(len(arrayElement2D)): for j in range(len(arrayElement2D[i])): print(arrayElement2D[i][j]) |
This will output:
Python array append
1 2 3 4 5 6 7 |
arrayElement = ["One", 2, 'Three' ] arrayElement.append('Four') arrayElement.append('Five') for i in range(len(arrayElement)): print(arrayElement[i]) |
The new element Four and Five will be appended at the end of the array.
1 2 3 4 5 6 7 |
One 2 Three Four Five |
You can also append an array to another array. The following code shows how you can do this.
1 2 3 4 5 6 |
arrayElement = ["One", 2, 'Three' ] newArray = [ 'Four' , 'Five'] arrayElement.append(newArray); print(arrayElement) |
1 2 3 |
['One', 2, 'Three', ['Four', 'Five']] |
Now our one-dimensional array arrayElement turns into a multidimensional array.
Python array size
We can use len
function to determine the size of an array. Let’s look at a simple example for python array length.
1 2 3 4 5 6 7 8 |
arr = ["One", 2, 'Three' ] arr2d = [[1,2],[1,2,3,4]] print(len(arr)) print(len(arr2d)) print(len(arr2d[0])) print(len(arr2d[1])) |
Python array slice
Python provides a special way to create an array from another array using slice notation. Let’s look at some python array slice examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
arr = [1,2,3,4,5,6,7] #python array slice arr1 = arr[0:3] #start to index 2 print(arr1) arr1 = arr[2:] #index 2 to end of arr print(arr1) arr1 = arr[:3] #start to index 2 print(arr1) arr1 = arr[:] #copy of whole arr print(arr1) arr1 = arr[1:6:2] # from index 1 to index 5 with step 2 print(arr1) |
Below image shows the python array slice example program output.
Python array insert
We can insert an element in the array using insert()
function.
1 2 3 4 5 6 |
arr = [1,2,3,4,5,6,7] arr.insert(3,10) print(arr) <img class="alignnone wp-image-22982 size-full" src="http://all-learning.com/wp-content/uploads/2017/08/python-array-insert1.png" alt="python-array-insert" width="1200" height="628" /> |
Python array pop
We can call the pop function on the array to remove an element from the array at the specified index.
1 2 3 4 5 6 7 |
arr = [1,2,3,4,5,6,7] arr.insert(3,10) print(arr) arr.pop(3) print(arr) |
That’s all about python array and different operations we can perform for the arrays in python.