≡ Menu

15 Python Array Examples – Declare, Append, Index, Remove, Count

An array is a data structure that stores values of same data type. In Python, this is the main difference between arrays and lists.

While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to same data type. In this tutorial, we will understand the Python arrays with few examples.

If you are new to Python, get started with the Python Introduction article.

To use arrays in python language, you need to import the standard ‘array’ module. This is because array is not a fundamental data type like strings, integer etc. Here is how you can import ‘array’ module in python :

from array import *

Once you have imported the ‘array’ module, you can declare an array. Here is how you do it:

arrayIdentifierName = array(typecode, [Initializers]

In the declaration above, ‘arrayIdentifierName’ is the name of array, ‘typecode’ lets python know the type of array and ‘Initializers’ are the values with which array is initialized.

Here is a real world example of python array declaration :

my_array = array('i',[1,2,3,4])

In the example above, typecode used is ‘i’. This typecode represents signed integer whose size is 2 bytes.

Typecodes are the codes that are used to define the type of array values or the type of array. Here is the list of available typecodes:

  • ‘b’ -> Represents signed integer of size 1 byte
  • ‘B’ -> Represents unsigned integer of size 1 byte
  • ‘c’ -> Represents character of size 1 byte
  • ‘u’ -> Represents unicode character of size 2 bytes
  • ‘h’ -> Represents signed integer of size 2 bytes
  • ‘H’ -> Represents unsigned integer of size 2 bytes
  • ‘i’ -> Represents signed integer of size 2 bytes
  • ‘I’ -> Represents unsigned integer of size 2 bytes
  • ‘w’ -> Represents unicode character of size 4 bytes
  • ‘l’ -> Represents signed integer of size 4 bytes
  • ‘L’ -> Represents unsigned integer of size 4 bytes
  • ‘f’ -> Represents floating point of size 4 bytes
  • ‘d’ -> Represents floating point of size 8 bytes

On a related topic, you should also know how to use Python Lists effectively.

1. Basic example

Here is a simple example of an array containing 5 integers

~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from array import *
>>> my_array = array('i', [1,2,3,4,5])
>>> for i in my_array:
... print(i)
...
1
2
3
4
5

So this way we can create a simple python array and print it.

2. Access individual elements through indexes

Individual elements can be accessed through indexes. Here is an example :

>>> my_array[1]
2
>>> my_array[2]
3
>>> my_array[0]
1

Remember that indexes start from zero.

3. Append any value to the array using append() method

Here is an example :

>>> my_array.append(6)
>>> my_array
array('i', [1, 2, 3, 4, 5, 6])

So we see that the value ‘6’ was appended to the existing array values.

4. Insert value in an array using insert() method

We can use the insert() method to insert a value at any index of the array. Here is an example :

>>> my_array.insert(0,0)
>>> my_array
array('i', [0, 1, 2, 3, 4, 5, 6])

In the above example, using insert() method, the value 0 was inserted at index 0. Note that the first argument is the index while second argument is the value.

5. Extend python array using extend() method

A python array can be extended with more than one value using extend() method. Here is an example :

>>> my_extnd_array = array('i', [7,8,9,10])
>>> my_array.extend(my_extnd_array)
>>> my_array
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

So we see that the array my_array was extended with values from my_extnd_array.

6. Add items from list into array using fromlist() method

Here is an example:

>>> c=[11,12,13]
>>> my_array.fromlist(c)
>>> my_array
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])

So we see that the values 11,12 and 13 were added from list ‘c’ to ‘my_array’.

7. Remove any array element using remove() method

Here is an example :

>>> my_array.remove(13)
>>> my_array
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

So we see that the element 13 was removed from the array.

8. Remove last array element using pop() method

Here is an example :

>>> my_array.pop()
12
>>> my_array
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

So we see that the last element 12 was popped out of array.

9. Fetch any element through its index using index() method

Here is an example :

>>> my_array.index(5)
5

So we see that the value at index 5 was fetched through this method.

10. Reverse a python array using reverse() method

Here is an example :

>>> my_array.reverse()
>>> my_array
array('i', [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

So we see that the complete array got reversed.

11. Get array buffer information through buffer_info() method

This method provides you the array buffer start address in memory and number of elements in array. Here is an example:

>>> my_array.buffer_info()
(33881712, 12)

So we see that buffer start address and number of elements were provided in output.

12. Check for number of occurrences of an element using count() method

Here is an example :

>>> my_array.count(11)
1

So we see that the element 11 occurred only once in the array.

13. Convert array to string using tostring() method

Here is an example :

>>> my_char_array = array('c', ['g','e','e','k'])
>>> my_char_array
array('c', 'geek')
>>> my_char_array.tostring()
'geek'

So we see that the character array was converted to string using this method.

14. Convert array to a python list with same elements using tolist() method

Here is an example :

>>> c = my_array.tolist()
>>> c
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

So we see that list ‘c’ was created by using tolist() method on my_array.

15. Append a string to char array using fromstring() method

Here is an example :

>>> my_char_array.fromstring("stuff")
>>> my_char_array
array('c', 'geekstuff')

So we see that the string “stuff” was added to my_char_array.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 7 comments… add one }

  • kamal babu August 14, 2013, 8:26 pm

    great tutorial but out of my reach.
    can you post some tutorials on Qt .

  • Tim September 13, 2013, 5:16 pm

    Isn’t the ‘array’ module outdated? Today, ‘numpy’ and ‘scipy’ are used. There is also ‘pylab’ (for MATLAB compatibility, and not just arrays), but pylab’s array operations behave differently that numpy’s/scipy’s.

  • tim zymurgy October 9, 2013, 8:55 am

    in example 1, using the iterator ‘i’ is a bit confusing. It caught me out on first read as your array is an array of integers i.e. ‘i’. It would be better to use something that is not a typecode identifier.

  • user4333 October 22, 2013, 10:56 am

    In example 7 it’s not clear whether ‘remove’ removes element with value 13 or at position 13.

  • aditya January 4, 2015, 6:25 pm

    i want to use a for loop to add values by the user using input command in an array….how can i do that?

  • Josh February 23, 2015, 8:32 am

    Gr8 help guys luv u all

  • Ray Huckfield September 24, 2015, 7:58 am

    I like this guide

Leave a Comment