Lately, I’ve caught myself using lists and arrays interchangeably. Specifically thinking of Python, both seem similar. Even dictionaries at least accomplish the same goal of storing information in a structure you can iterate through. But there’s also a list of differences between them. So that’s what this article will talk about today.
First, we’ll describe arrays, lists, and dictionaries, then finally we’ll use a brief comparison of the differences between the three.
The only comparison we won’t make is with the syntax, as it is a given that they would need to have different syntaxes to work. So, without any further delay, let’s dive straight in.
An array is a special type of variable that can hold more than one value at a time. To do this, it creates a form of a list, where you can iterate through all the different values.
To sort through this type, there are indexes. An index simply assigns a number (starting at zero) that gives the array a kind of predetermined order based on when values are added. You can sort it later though with a
sort method. You can also add and remove later.
The only thing about arrays in Python you should keep in mind is that the Array type isn’t automatically a data type you can use. You must import the array type. But because an array assigns an index, duplicate values are allowed to be added to this type.
A list is a data type that allows the storage of multiple values, or items, in one variable. It is automatically a data type in Python, so you can begin using it whenever you want.
The lists are ordered by when they are entered into the list, but that can be changed later. Lists are also indexed, starting at zero, and counting so you can iterate through the list.
Because the implicit index is given to each value, you can have duplicate values within the list. You can sort the list with the sort method as well.
One unique capability of a list is that it can contain values of different types. That means you can store values such as strings and integers within the same list.
A dictionary is another data type that allows you to store multiple values into a single data type. The same as the other two types discussed, dictionaries have a predetermined order of items based on when they are added to the variable.
However, one big difference between the dictionary data type is that it does not use an index. Instead, values are stored in a key-value pair. When thinking about
key:value, it looks more like what a record in a database would do. Every value is prefaced with the type of value it is. This data type also enforces that there cannot be duplicate values.
Now that we know a little bit more about each data type, let’s look at some of the differences between them. Firstly, as discussed earlier, to use an array you must import a special module.
For both lists and dictionaries, you can use the data types as just a regular extension of Python, nothing special to do there.
In an array, all data types must have the same values. For example, if you start with strings, even an integer would need to be in a string format, as all values must be the same data type.
In the list, there can be different data types for the values. Dictionaries have the key feature, so the actual value does not matter as much as matching the keys between values.
Both arrays and lists use indexes to differentiate the values stored in some order. But a dictionary uses a
key:value instead. This means that arrays and lists can allow duplicate values, which dictionaries do not. These indexes also cause be used as their order, so you can reverse the order with the “reverse” method in both arrays and lists.
A dictionary doesn’t have a reverse method, as it has keys with the values instead of an index. One other note is that a dictionary does not guarantee that the order of entered information will always stay the same. That is unlike arrays and lists, which maintain their order because they are numbered by the indexes.
Arrays can also be used to directly handle arithmetic operations. It is the only data type to handle the operations so directly. Others can have mathematics performed on the data, but it is not as direct as with an array.
With a list, you don’t have to iterate through every value to print. You can just print the entire list at once. That’s unlike an array, where you would need to iterate through the values. You would also need to loop through a dictionary. Otherwise, you could create a temporary list of the dictionary values, then print them at once. But without creating extra work, you would need to loop through the values like you would for an array.
The final element we’ll look at is the memory involved. So, lists tend to consume more memory, as they’re made to have easier adding and removing from the list. Because of this, they also prefer shorter sequences of data. For arrays, they are built to hold longer sequences of data, so they tend to be more compact.
Dictionaries are made to store large amounts of data, but could also eat up more memory than you want them to, and they may run out of their allotted memory.
This has just been a brief look at lists, arrays, and dictionaries. Although they all hold multiple values in a single variable, they are all different as well. All we discussed is only looking at the Pythonic versions of these data types.
Mainly, the article was to just take a chance to remember why the data types are so different, and even a little of why to choose one over the other by remembering their capabilities. We just didn’t look at the differences in syntax, mainly because we understand they all must be different to exist.
In Python, you may be considering mainly either lists or dictionaries, depending on the type of data you’re storing, simply because an array would take a special import to work. However, each of these data types has its perks and reasons why to be selected.
I hope you found this reminder interesting, and please feel free to comment on any differences I missed. Until next time, cheers!
Also published here.