array: a fast idiot-savant list

Disappointment ran through me like beer at a keg party when I got a third of the way into the documentation at https://docs.python.org/3/library/array.html#module-array . The array data container being described was NOT what I call an array.

What Kind of Data Container is array?
Array is just sort of an idiot-savant list. Technically, it is a one dimensional unitary type data vector – no n dimensional support, not even a little nod to a matrix. What that means, when you banish the geek-speek, is that you must choose one of thirteen different “types” of data to be the one and only one you will use when you create your array, and your data elements will be stored in a long, flat, linear stream. On the positive side, this design lets array be super efficient and fast at doing what it does.

The array Syntax
array.array(‘typecode string’ [, initial data of type ‘typecode string’])
(^class)        (^like ‘i’ – must be in quotes)
example:  array.array(‘i’,[1.2.3.4.5])

Data Types
Before I mislead you, of the thirteen data types you can choose, one type (“u”) is obsolete and two (“q” and “Q” signed and unsigned “long long integers”) are reportedly unreliable and besides that they only work on 64 bit versions of Windows. “u” was arguably the most interesting of the data types but it’s gone – no sense crying over spilled milk. Of the ten remaining, six would only be of interest to programmers who have no business wasting their time reading this article. The remaining four, “i” and “I” (signed and unsigned integer) and “f” (float) and “d” (double – requires 8 bytes of storage) are what 99.999% (log 5 or about 4 standard deviations) of us might want, and be able, to use.

Some Useful array Functions
array.array(typecode[, initializer])
myArray = array(‘i’, [1,2,3,4])
.typecodes
…yeilds: ‘bBuhHiIlLqQfd’
.typecode – char used to create current array, list “i” or “f”
.itemsize – bytes of an item
.append(item) – add a new item
.count(x) – number of times x is in array
.extend(iterable) – append iterable contents
.fromlist(list) – append list items
.index(x) – index of 1st x
.insert(i,x) – put new item x in array position i
.pop([i]) – remove item indexed by i and return it
.remove(x) – remove first x
.reverse() – reverse order of items
.tolist – convert array to a list

A Small Quandary
My experimentation with arrays has convinced me there is a bit of a curious conflict depending on how you start off with the import statement. Note the python.org documentation does not address importing array. I discovered something similar with tkinter but that issue only seems to apply in cross-platform situations.
The problem I am about to describe can be demonstrated on any (I think) Windows system and is trivial as far as I know, but may extend beyond what I have encountered.
It appears there is an array command at the module level as well as an array function, plus some other options
Open Python and give the command “import array”. Now type ‘array.typecodes’. You will get back the string: ‘bBuhHiIlLqQfd’ …which is the list of codes for all possible types of data that array can support.
Now close and reopen Python, enter “from array import array” or “from array import *” and then once again give the “array.typecodes” command. You will be greeted with:
Traceback (most recent call last):
File “”, line 1, in
AttributeError: type object ‘array.array’ has no attribute ‘typecodes’
…. But if you now just type the word “typecodes” at the prompt (not array.typecodes) you will get:
>>> typecodes
‘bBuhHiIlLqQfd’
>>>

And this dual interface works/doesn’t work in more than immediate mode. Note the interplay between how the import command is given and how the array function is called and works – or doesn’t.

Our hunch is that the safest course it to use “import array”, and force all the standard function structure calls, even though the shorter method is, well, shorter.

Here is a bit of code as array() examples:

import array
myArray = array.array('i',[0,1,2,3,4,5,6,7,8,9])
print('an aray:')
for i in myArray:
    print (i, end=", ")
print("\nReverse the list.")
myArray.reverse()
for i in myArray:
    print (i, end=", ")
print("\nNow lets pop the last number off.")
x=myArray.pop()
print("the popped value was: ",str(x))
print('new array: ')
for i in myArray:
    print (i, end=", ")
print('\nWe can also iterate the array')
testiter=iter(myArray)
for i in range(10):
    print(next(testiter,"that's all folks"),end=', ')
print()
print('\n plus typecodes just for fun:')
print(array.typecodes)