Search
Common Data Structures: Lists, Tuples, and Dictionaries

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.

List

Python lists are used to store a collection of data. For example, a collection of integers:

Notice Python lists use square brackets [] and separate the entries with commas.

x = [1, 2, 6, 4]
x
[1, 2, 6, 4]

Or a collection of floating point numbers:

x = [1.1, 3.0]
x
[1.1, 3.0]

Or even heterogeneous data, like an integer and a floating point number and a string:

x = [1, 1.3, 'string']
x
[1, 1.3, 'string']

You can even have list of lists, and those can be heterogeneous as well

x = [['a list', 'of strings'],[1,2,3]]
x
[['a list', 'of strings'], [1, 2, 3]]

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]
'of strings'

You can also perform operations on lists, e.g. appending values

x = [1,2,3]
x.append(4)
x
[1, 2, 3, 4]

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
[1, 2, 10, 3, 4]

Or sorting

Notice the sorting occurs in place, i.e. the order of x is permanently changed.

x.sort()
x
[1, 2, 3, 4, 10]

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]]
[['a list', 'of strings'], [1, 2, 3], [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 Oth index.

x[0] = ['another list']
x
[['another list'], [1, 2, 3]]

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.

Tuple

Tuples are kind of like constant list. If you have data that you want to store in a structure, but you know it will not be changed, tuples can be more efficient. The syntax for tuples is given in the example below:

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)
1
2
3

You cannot reassign values in a tuple. They are immutable!

x[0] = 4
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-4fb93c654a12> in <module>
----> 1 x[0] = 4

TypeError: 'tuple' object does not support item assignment

Dictionary

Dictionaries are a way to efficiently store and retrieve keyword: value type data. For example:

my_dict = {'numerical' : 1, 'alphabetical': 'abc'}

Then we can look up the value of 'numerical' quickly:

my_dict['numerical']
1

Or 'alphabetical'

my_dict['alphabetical']
'abc'

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']
'gmres'

Another way to access parts of a dictionary is the with get function, e.g.

my_dict.get('alphabetical')
'abc'

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']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-21-6a9c34cc7f98> in <module>
----> 1 my_dict['name']

KeyError: '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')
'Romeo'

Further reading

Further reading on data structures can be found in the Python documentation.