Python lists, or arrays, are fairly simple to create in Python. Lists are defined as a basic data type in Python and therefore to create a list of items in python, we simply define the list using square brackets. To create an empty list, we can simply use square brackets.
x = []
This is an array that has no items in it. We can begin adding items to the empty array by appending or extending the array. Let’s take a look at the difference between these two functions. The functions work as follows:
x = [] x.append(1) x.extend(2)
By running the above code we get [1,2] in return. In the case of inserting a single value, the append and extend functions have no differences. However, when adding an array of elements, they work differently.
x = [1,2] x.append([3,4]) # x = [1,2,[3,4]] x.extend([5,6]) # x = [1,2,[3,4],5,6]
Notice in this example, that the append function adds the array as an array in the 3rd element. The extend function on the other hand adds the list of elements as an extension of the original list, x.
Another task that might be necessary is creating an array that defaults to all 0s. This can be done as follows, where SIZE is a variable that defines size. You simply multiply an array list of zeros by the desired size. You can also do this to create a multidimensional array as well. To create a 3-by-3 2D array of zeros. We create a list of item within the larger list multiplying the dimensions to the necessary sizes as shown in the second part below.
SIZE = 20 x = [0]*SIZE # x = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] DIMX = 3 DIMY = 3 y = [[0] * DIMX] * DIMY # y = [0, 0, 0], [0, 0, 0], [0, 0, 0]]
As in most languages, accessing the items of a list can be done by providing the key of the item that you would like to access. Remember, Python is a languages which begins with 0 as the first element of the list. Therefore, if we want to access the list within the list in the above example (or the third element) we want to use the key of 2.
x = [1,2,[3,4],5,6] print(x[2]) # output >> [3,4]
One other function to discuss here is getting the length of an array list. This is relatively simple as the function len( ) provides a means of getting the size of the array. This function outputs the size of a list.
x = [1,2,[3,4],5,6] size = len(x) print(size)
Creating a Multidimensional array can be done by creating lists for elements within the list. The above example creates a single element x[2] that contains a multidimensional aspect. If we wanted to access the value 4, we could do this using x[2][1].
One benefit of python arrays as opposed to other languages is that mixed values can be used; however, in my opinion, it is generally not good to mix the types unless absolutely necessary. By avoiding mixed arrays, you ensure that when processing the array you do not run into potential errors due to an unexpected data type.
For even more information about lists (as python lists provide even more tools for processing lists). Here are a list of a few good resources: