XClose
Menu

Containers

Checking for containment.

The list we saw is a container type: its purpose is to hold other objects. We can ask python whether or not a container contains a particular item:

In [1]:
'Dog' in ['Cat', 'Dog', 'Horse']
Out[1]:
True
In [2]:
'Bird' in ['Cat', 'Dog', 'Horse']
Out[2]:
False
In [3]:
2 in range(5)
Out[3]:
True
In [4]:
99 in range(5)
Out[4]:
False

Mutability

A list can be modified:

In [5]:
name = "James Philip John Hetherington".split(" ")
print(name)
['James', 'Philip', 'John', 'Hetherington']
In [6]:
name[0] = "Dr"
name[1:3] = ["Griffiths-"]
name.append("PhD")

print(" ".join(name))
Dr Griffiths- Hetherington PhD

Tuples

A tuple is an immutable sequence. It is like a list, execpt it cannot be changed. It is defined with round brackets.

In [7]:
x = 0,
type(x)
Out[7]:
tuple
In [8]:
my_tuple = ("Hello", "World")
my_tuple[0] = "Goodbye"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-242e9dae76d3> in <module>
      1 my_tuple = ("Hello", "World")
----> 2 my_tuple[0] = "Goodbye"

TypeError: 'tuple' object does not support item assignment
In [9]:
type(my_tuple)
Out[9]:
tuple

str is immutable too:

In [10]:
fish = "Hake"
fish[0] = 'R'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-7127277fc72e> in <module>
      1 fish = "Hake"
----> 2 fish[0] = 'R'

TypeError: 'str' object does not support item assignment

But note that container reassignment is moving a label, not changing an element:

In [11]:
fish = "Rake" ## OK!

Supplementary material: Try the online memory visualiser for this one.

Memory and containers

The way memory works with containers can be important:

In [12]:
x = list(range(3))
x
Out[12]:
[0, 1, 2]
In [13]:
y = x
y
Out[13]:
[0, 1, 2]
In [14]:
z = x[0:3]
y[1] = "Gotcha!"
In [15]:
x
Out[15]:
[0, 'Gotcha!', 2]
In [16]:
y
Out[16]:
[0, 'Gotcha!', 2]
In [17]:
z
Out[17]:
[0, 1, 2]
In [18]:
z[2] = "Really?"
In [19]:
x
Out[19]:
[0, 'Gotcha!', 2]
In [20]:
y
Out[20]:
[0, 'Gotcha!', 2]
In [21]:
z
Out[21]:
[0, 1, 'Really?']

Supplementary material: This one works well at the memory visualiser.

The explanation: While y is a second label on the same object, z is a separate object with the same data. Writing x[:] creates a new list containing all the elements of x (remember: [:] is equivalent to [0:<last>]). This is the case whenever we take a slice from a list, not just when taking all the elements with [:].

The difference between y=x and z=x[:] is important!

Nested objects make it even more complicated:

In [22]:
x = [['a', 'b'] , 'c']
y = x
z = x[0:2]
In [23]:
x[0][1] = 'd'
z[1] = 'e'
In [24]:
x
Out[24]:
[['a', 'd'], 'c']
In [25]:
y
Out[25]:
[['a', 'd'], 'c']
In [26]:
z
Out[26]:
[['a', 'd'], 'e']

Try the visualiser again.

Supplementary material: The copies that we make through slicing are called shallow copies: we don't copy all the objects they contain, only the references to them. This is why the nested list in x[0] is not copied, so z[0] still refers to it. It is possible to actually create copies of all the contents, however deeply nested they are - this is called a deep copy. Python provides methods for that in its standard library, in the copy module. You can read more about that, as well as about shallow and deep copies, in the library reference.

Identity vs Equality

Having the same data is different from being the same actual object in memory:

In [27]:
[1, 2] == [1, 2]
Out[27]:
True
In [28]:
[1, 2] is [1, 2]
Out[28]:
False

The == operator checks, element by element, that two containers have the same data. The is operator checks that they are actually the same object.

But, and this point is really subtle, for immutables, the python language might save memory by reusing a single instantiated copy. This will always be safe.

In [29]:
"Hello" == "Hello"
Out[29]:
True
In [30]:
"Hello" is "Hello"
Out[30]:
True

This can be useful in understanding problems like the one above:

In [31]:
x = range(3)
y = x
z = x[:]
In [32]:
x == y
Out[32]:
True
In [33]:
x is y
Out[33]:
True
In [34]:
x == z
Out[34]:
True
In [35]:
x is z
Out[35]:
False