Python Array With Examples

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.

Output of above one dimensional array example program will be:

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.

It give the following output:

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.

It will produce the following output:

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.

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.

This will output:


Python array append

The new element Four and Five will be appended at the end of the array.

You can also append an array to another array. The following code shows how you can do this.

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.

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.

Below image shows the python array slice example program output.


Python array insert

We can insert an element in the array using insert() function.

Python array pop

We can call the pop function on the array to remove an element from the array at the specified index.

That’s all about python array and different operations we can perform for the arrays in python.

By admin

Leave a Reply

%d bloggers like this: