Python @ DjangoSpin

Python: Arrays

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 2 minutes

Arrays in Python

Arrays in Python

Arrays in Python: An array is a data structure which stores homogeneous elements i.e. of the same type. This is in contrast to lists in Python, which can store elements of different types. While lists are a native data structure in Python, arrays are not. In this article, we'll look at how to use arrays with the help of the standard library array. We'll look at various operations supported by Python arrays using primitive examples. Let's begin.

Arrays in Python

Table of Contents

Declaring an array

>>> import array
>>> myArray = array.array('i', [1, 2, 3, 4, 5, 6, 7, 8])

The constructor of class 'array' takes 1 positional argument i.e. typecode & 1 optional keyword i.e. initializer. The typecode is a single character string which tells Python about the data type of elements consisting the array. Listed below are the various typecodes and the type of data they represent. The initializer can be either of a list, a bytes-like object or an iterable.

	'b'					signed integer 							 		1 byte
	'B'					unsigned integer								1 byte
	'c'					character 										1 byte
	'u'					unicode character								2 bytes
	'h'					signed integer									2 bytes
	'H'					unsigned integer								2 bytes
	'i'					signed integer									2 bytes
	'I'					unsigned integer								2 bytes
	'w'					unicode character 								4 bytes
	'l'					signed integer 									4 bytes
	'L'					unsigned integer								4 bytes
	'f'					floating point									4 bytes
	'd'					floating point									8 bytes

An array object, returned by the constructor of the array class, has a multitude of functions. These functions allow the user to append elements to the array, insert elements at specific positions of the array, extend an existing array etc.

Adding elements to an array: append() & insert()

The append() appends the said element to the end of the array. The insert() inserts the said element at the specified index of the array.

>>> myNameArray = array.array('u', 'Etha')
>>> myNameArray
array('u', 'Etha')
>>> myNameArray.append('n')
>>> myNameArray
array('u', 'Ethan')

>>> myNameArray.insert(0, '7')
>>> myNameArray
array('u', '7Ethan')

Removing elements from an array: remove() & pop()

The remove() removes the first occurrence of the said element in the array. The pop() removes the last element from the array and returns it.

>>> myNameArray = array.array('u', 'ETHAN HUNT')
>>> myNameArray.remove('H')
>>> myNameArray
array('u', 'ETAN HUNT')
>>> myNameArray.pop()
>>> myNameArray
array('u', 'ETAN HUN')

Accessing elements of an array: [index] & index()

You can access individual elements of an array using the index notation i.e. [ ]. In order to find the index of an element in the array, use the index() method. Note that in case of multiple occurrences, the index of first occurrence is returned.

>>> myNameArray = array.array('u', 'ETHAN HUNT')
>>> myNameArray[2]

>>> myNameArray = array.array('u', 'ETHAN HUNT')
>>> myNameArray.index('N')

Extending an array: extend()

In order to add an existing array to the tail of another array, use the extend() method.

>>> myNameArray = array.array('u', 'Ethan ')
>>> myLastNameArray = array.array('u', 'Hunt')
>>> myNameArray.extend(myLastNameArray)
>>> myNameArray
array('u', 'Ethan Hunt')

Adding elements from lists: fromlist()

To extend an array from elements of a list, use the fromlist() method.

>>> myNameArray = array.array('u', 'Ethan ')
>>> myLastNameList = ['H', 'u', 'n', 't']
>>> myNameArray.fromlist(myLastNameList)
>>> myNameArray
array('u', 'Ethan Hunt')

Reversing an array: reverse()

To reverse an array, use the reverse() method.

>>> myNameArray = array.array('u', 'Ethan')
>>> myNameArray.reverse()
>>> myNameArray
array('u', 'nahtE')

Number of occurrences of an element: count()

The count() method returns the number of occurrences of the mentioned element in the array.

>>> myNameArray = array.array('u', 'Ethan Hunt')
>>> myNameArray.count('n')

Converting an array to a list: tolist()

To convert an array to a list, use the tolist() method.

>>> myNameArray = array.array('u', 'Ethan Hunt')
>>> myNameArray.tolist()
['E', 't', 'h', 'a', 'n', ' ', 'H', 'u', 'n', 't']

Comparing arrays and lists

There are four major points of difference between arrays and lists:

  • Lists can be used for any type of objects, whereas arrays can only be used for a restricted set of objects.
  • Arrays restrict the type of data they contain, wheras lists have no such restrictions.
  • Arrays are more momory-wise efficient than lists.
  • For numerical computations, arrays are much more efficient than lists.

Further Reading

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply