Built into python itself is the list data type, and for most situations this seems to work well as an array in python. However it is not like lists are the only option when it comes to arrays, or sequence types in python, another built in feature is arrays. Arrays might not be there to begin with, but they can quickly be added by way of the array standard library. Lists might still work just fine in most situations, however I think I should take a moment to at least touch base on these when it comes to being aware at least of an alternative to lists, and the other sequence types that are built into python itself.
In this section I will be going over just a few quick, simple, getting started examples of arrays with the array standard library. That is just creating a single array of a single type, and just a few other basic examples that have to do with getting the current type of the array if it is not know to begin with.
For a basic getting started example of arrays in python, here is a basic example of a simple integer array.
By using the type built in function and the name property I can confirm the type of the array itself, and the data type of the elements. Because this is an integer array I can only add integer values to it. If I set an element to a value other than an integer that will result in an error. Also there is a limit when it comes to integer values, if I go over that value it will result in an error. Also this is not just an integer array, it is also an signed integer array, if I want unsigned integers I will want to use a capital I for the type code.
What if I am in a situation in which I need to know what the data type of the array that was created before hand. For this there is the type code of the array.
So it is important to know what the type codes are for arrays. With that said there is the type codes property of the array module. However this will just give a string of all the type codes, it will not explain what each of them are.
To really know which one is which a table is needed.
Now that I have covered some of the very basics of arrays in python, I think that I should move on to some more examples of arrays. This time I will be focusing on what there is to work with when it comes to functions in the array library. I will not be getting to all of them here, but I think I should cover some of the most important ones at least real quick.
Just like lists in python by itself these arrays are dense, not sparse. So I can not just set any index value I want to an array, I must stay within the range of the array. There are ways of increasing the range of the array though in place without creating a new array. Just like with lists there is also an append method, this will append a new element to the end of the array, increasing the range of the array in the process of doing so.
The append method is one way to go about adding additional elements to an array, but then there is the pop method which is the first method that comes to mind when it comes to removing elements from an array. By default it will remove the last element from an array, but an index value can be given as the first an only argument. So the pop method can be used to remove any element from an array, assuming that you know the index value to begin with.
The count method of an array instance is one way to get a count of a number of elements in an array that are of a given value.
There should be a way to create a list from an array, and onw way to go about doing so is the to list method.
Just with lists arrays to and be looped over with for a while loops. In this section I will just go over a few quick examples of looping and arrays.
One way to loop over the contents of an array is to use a for loop.
It would seem that these kinds of arrays can not be arrays of arrays. However I often make all of my arrays linear anyway even arrays that I want to make multidimensional.
I am not going to say that arrays should always be used over lists, or anything to that effect. There is no golden hammer when it comes to these types of things. In fact most of the time I am sure that I would prefer to use lists, it is just that in some situations I should probably use these arrays in place of lists.