Skip to content

Python Arrays Declaration | Methods | Examples

  • by

Python Arrays are sequence types, it’s similar to Lists, but the type of objects stored in the Arrays is constrained. Arrays are kind of variables a hold more than one value within the same variable and organized with indexing, where indexing starts with 0.

In this tutorial, you will learn about Python Arrays and it’s examples with arrays inbuilt functions in python.

Python Arrays Declaration | Methods | Examples

What is the difference between Lists and Arrays in Python?

Usually, if you say “array” when talking about Python, they mean “list”.

The list items can be anything and each list element can have a completely different type. But it’s not in arrays. Arrays are objects with definite type and size. The concept of the list is good, it makes their use of more lists flexible.


A simple syntax.

itmesArray = ["item1", "item2", "item3"]

Create a Python Arrays

it’s a simple array example in python, which holds integers (numbers).

array1 = [5, 10, 15, 40, 50]

Output : [5, 10, 15, 40, 50]

Get the elements from Array

You can access (get) individual elements of an array using index number in square brackets []. Like this example …

array1 = [5, 10, 15, 40, 50]

Output: 40

Note: Python Arrays is a zero-indexed,  means the element’s position starts from 0 instead of 1.

Find the length of an Array

You have to use the len() method to return the length of an array.

array1 = [5, 10, 15, 40, 50]

Output: 5

For Loop for Python Arrays

Run the loop over the Array and a print() every element example.

array1 = [5, 10, 15, 40, 50]

for a in array1:

Output: 5

Adding Elements in Array

The code adding elements in the array.

Use the append() method to add an element to an array.

array1 = [5, 10, 15, 40, 50]

Output : [5, 10, 15, 40, 50, 100]

Removing Elements in Array

Here is code for removing elements from python arrays.

Pass the index number of elements in  pop() method to remove an element from the array.

array1 = [5, 10, 15, 40, 50]

Output : [5, 10, 15, 50]

If you want to remove an element by its value not index the use the remove() method.

array1 = [5, 10, 15, 40, 50]

Output : [5, 15, 40, 50]

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Examples are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading