Search
Collections

Prerequisites

  • Types

Collections

Previously, we were introduced to individual types that exist in Python. Python types go beyond individual items, including objects that can hold collections of variables. This section will introduce three different collection types:

  • Lists
  • Tuples
  • Dictionaries

As is the case with individual types, different collection types can be acted on in different ways.


Lists

The first type of collection we will look at is a list. Let us look first at how to create a list.

noble_gases = ['helium', 'Neon', 'Argon', 'Krypton', 'Xenon', 'Radon']
type(noble_gases)
list
print(noble_gases)
['helium', 'Neon', 'Argon', 'Krypton', 'Xenon', 'Radon']

We can use the index notation to identify particular elements in a list. However, it is important to note that this notation counts from $0$, so the first element of the list is found with,

print(noble_gases[0])
helium

The index notation is shown for a list of the first 7 elements in the figure below.

We can also change particular values, as lists are mutable.

noble_gases[0] = 'Helium'

The final element in the tuple therefore has the index of $n-1$, where $n$ is the tuple length. The lengths of a collection can be found with the len() function.

len(noble_gases)
6
print(noble_gases[len(noble_gases)-1])
Radon

You may notice that Oganesson is missing from the list, it is possible to concatenate it.

noble_gases = noble_gases + ['Oganesson']
print(noble_gases)
['Helium', 'Neon', 'Argon', 'Krypton', 'Xenon', 'Radon', 'Oganesson']

In addition to standard indexing, it is also possible to perform negative indexing with lists and tuples, where $-1$ is the final element of the list with the counting up moving backwords through the list.

print(noble_gases[-1])
Oganesson

We can also get slices of a list, this is achieved using the : notation.

print(noble_gases[1:3])
['Neon', 'Argon']

This notation can be a bit confusing, as this notation is inclusive then exclusive. The slice of the list includes the element with the index that preceeds the colon and exclusive of that after the colon. In the example above, we can see that is includes the index $1$ item ('Neon') but excludes the index $3$ element ('Krypton').

This is shown for the elements list in the figure below.

It is also possible to use list slicing to skip elements of the list, where the number after the second colon is the step size.

print(noble_gases[1:5:2])
['Neon', 'Krypton']

Above, the second to the sixth elements are printed skipping each second (so those with indices $2$ and $4$).

As well as list concatentation (the adding together of lists above), it is possible to append single elements to the end of a list.

halogens = ['Fluorine', 'Chlorine', 'Bromine', 'Iodine']
print(halogens)
['Fluorine', 'Chlorine', 'Bromine', 'Iodine']
halogens.append('Astanine')
print(halogens)
['Fluorine', 'Chlorine', 'Bromine', 'Iodine', 'Astanine']
halogens.append('Tennessine')
print(halogens)
['Fluorine', 'Chlorine', 'Bromine', 'Iodine', 'Astanine', 'Tennessine']

Finally, we note that a list does not have to be of a consistent type. So it is possble for a list to be made up of disparate forms of information.

chlorine = ['Cl', 17, 35.45]
print(chlorine)
['Cl', 17, 35.45]

Where the list chlorine contains information about the chemical symbol, atomic number, and the average mass number.


Tuples

The immutable cousin to a list is a tuple, a tuple acts in many ways the same as a tuple (such as indexing). However, the elements of a tuple cannot change once the object is defined. Let's look at how a tuple is defined.

noble_gases = ('helium', 'neon', 'argon', 'krypton', 'xenon', 'radon')

Above is a tuple of noble gases, where the tuple is defined using round brackets.

type(noble_gases)
tuple

Like with lists, we can use the index notation to identify particular elements in the tuple.

print(noble_gases[0])
helium
print(noble_gases[-1])
radon

Above it was mentioned that a tuple is an immutable type, that once created it's objects cannot be altered. We can see this in action if we try and change the value of one of the elements in the tuple.

noble_gases[0] = 'Helium'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-22efed8af598> in <module>
----> 1 noble_gases[0] = 'Helium'

TypeError: 'tuple' object does not support item assignment

However, it is still possible to concatenate tuples, like lists, but the item that is being concatentated must be a tuple, hence the brackets and trailing comma.

noble_gases + ('oganesson',)
('helium', 'neon', 'argon', 'krypton', 'xenon', 'radon', 'oganesson')

The immutability of a tuple does not mean that the whole variable cannot be overwritten.

noble_gases = 'inert'
print(noble_gases)
inert

Additionally, like lists, tuple items can also consist of data of different types.


Dictionaries

The final type of collection we will discuss here is the dictionary (dict). This collection type consists of keys and values. These keys and values are analogous to the words and definitions of a traditional dictionary. Consider if we rewrite the chlorine variable as a dictionary.

chlorine = {'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45}
print(chlorine)
{'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45}

Here, we are able to use the keys of the dictionaries as labels for each datum.

print(chlorine.keys())
dict_keys(['chemical symbol', 'atomic number', 'average mass number'])

It is possible to access a single one of the elements of a dictionary, by passing the relevant key in lieu of an index.

chlorine['atomic number']
17

However, traditional indexing does not work for dictionaries, as the index is not a key.

chlorine[1]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-30-a0685985ddc1> in <module>
----> 1 chlorine[1]

KeyError: 1

We can add elements to a dictionary rather simply.

chlorine['number of isotopes'] = 2
print(chlorine)
{'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45, 'number of isotopes': 2}

Finally, the items in a dictionary need not be single variables, like a float or an int. They can also be a list or tuple.

chlorine['mass numbers'] = (35, 37)
print(chlorine)
{'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45, 'number of isotopes': 2, 'mass numbers': (35, 37)}

Exercise

Add the necessary information to the dictionary below such that it is a dictionary where the keys are the names of the first-row of transition metals, and the values are int of the number of valence electrons. Test your dictionary by printing some of the values.

d_block = {}