A data structure is a term used in computer science to describe a type of "data container". They are used to store multiples of (usually) the same type of data and provide certain accessor functions to extract the data from the container. Different data structures have varying properties that trade off ease of use, speed of access, memory footprint, etc.
Below we cover a few data structures in the Python language. There are a few more not covered here, but these are by far the most common.
Notice Python lists use square brackets []
and separate the entries with commas.
x = [1, 2, 6, 4]
x
Or a collection of floating point numbers:
x = [1.1, 3.0]
x
Or even heterogeneous data, like an integer and a floating point number and a string:
x = [1, 1.3, 'string']
x
You can even have list of lists, and those can be heterogeneous as well
x = [['a list', 'of strings'],[1,2,3]]
x
Lists support indexing to extract the data stored in them.
The [0]
takes the first entry in x
which is itself a list containing ['a list', 'of strings']
. The second [1]
takes the 1
-index entry in that list 'of strings'
. In Python, counting starts from 0.
x[0][1]
You can also perform operations on lists, e.g. appending values
x = [1,2,3]
x.append(4)
x
Or inserting into the middle of them.
The 2
indicates the index location where the insertion should occur. The 10
is the value to insert.
x.insert(2, 10)
x
Or sorting
Notice the sorting occurs in place, i.e. the order of x
is permanently changed.
x.sort()
x
There is a shorthand syntax for appending lists to other lists. Just as we can add numbers to other numbers and strings to other strings, we can add two lists together.
x = [['a list', 'of strings'],[1,2,3]]
x + [[1.1, 2.2]]
You can also use the indexing syntax for reassignment. For example, to replace the first position of the list x
, you can use the O
th index.
x[0] = ['another list']
x
Lists are mutable! Meaning they can be changed, extended, appended, inserted into, sorted, etc. In some languages, functions that change a data structure are called mutator or setter functions, and functions that extract data from a data structure are called accessor or getter functions.
Notice the surrounding parentheses ()
on a tuple.
x = (1, 2, 3)
There is an effiecent "unpacking" syntax for tuples.
Here the first entry in the tuple gets assigned to a
, the second to b
, and the third to c
.
a, b, c = x
print(a)
print(b)
print(c)
You cannot reassign values in a tuple. They are immutable!
x[0] = 4
my_dict = {'numerical' : 1, 'alphabetical': 'abc'}
Then we can look up the value of 'numerical'
quickly:
my_dict['numerical']
Or 'alphabetical'
my_dict['alphabetical']
Just likes lists, you can have a dictionary-of-dictionaries
my_dict = {'numerical' : {'linear solver' : 'gmres'},
'alphabetical': 'abc'}
To return the value of 'linear solver'
directly
my_dict['numerical']['linear solver']
Another way to access parts of a dictionary is the with get
function, e.g.
my_dict.get('alphabetical')
One advantage the get
function has over the bracket []
syntax is that it will not error if the keyword does not exist in the dictionary, e.g.
my_dict['name']
my_dict.get('name')
Of course, it does not return a value if 'name'
is not defined in the dictionary, but the get()
function allows for setting a default value in the event the keyword is undefined
my_dict.get('name', 'Romeo')
Further reading
Further reading on data structures can be found in the Python documentation.