Python: Lists, Tuples and Sets

List

Let’s create a simple list

Python

Now let’s type in the list1 followed by a dot and we can see a number of functions which we can call from list 1.

This image has an empty alt attribute; its file name is vlcsnap-2019-10-12-23h34m05s016.png

For instance we can use index to find the index of the value 2:

Python
1

Recall that because we use zero order indexing, the value one is index 0, the value 2 is index 1 and so on.

Next we can use the function insert, to insert a single value at a given index. Let us at index=1 set a value=5. Here the 0th input argument is the index and the 1st input argument is the value 5. All original values at the original index or later are increased by 1.

Python
[1, 5, 2, 3, 4]

The list can be reversed using the function reverse, this has no input arguments, however to call the function the brackets () are required so the brackets are empty.

Python
[4, 3, 2, 5, 1]

The list can also be sorted, to sort in numerical/alphabetical order, no input arguments are required.

Python
[1, 2, 3, 4, 5]

However if we want to sort in reverse numerical/alphabetical order, we can specify a keyword input argument and assign it to the Boolean True.

Python
[5, 4, 3, 2, 1]

Note this input keyword argument has a default of False, so when it is not called, it will be assigned to False and sort in numeric/alphabetical order.

We can remove a value from the list, here the input argument is the value, in this case 1 and not the index (which was at 4).

Python
[5, 4, 3, 2]

We can use it again to remove the value 4, at index 2.

Python
[5, 3, 2]

The function pop, pops off the last value in a list. It can be assigned to a variable:

Python
popped=1
list=[5, 3]

The function clear has no input arguments and clears the list, leaving it empty:

Python
[]

Let’s now go ahead and create list1 and list2:

Python

We can append the list2 to the end of the list1 using the function append on list1 with list2 as the input argument:

Python
[1, 2, 3, 4, [5, 6, 7, 8]]

Note the function append adds the entire list2 as the new last element of the list1, i.e. makes a nested list2.

We can relaunch the script and start with list1 and list2 again.

This time instead of using the function append, we can use the function extend.

Python
[1, 2, 3, 4, 5, 6, 7, 8]

This adds each element to the end of list 1 as a separate element.

We can count how many times, the value 1 occurs in a list by using the function count and the input argument 1.

Python
1

It only occurs once so we get a value of 1 returned.

We can then insert at index 1, a value of 1.

Python

Now recount:

Python
2

And it is updated to 2 as expected.

Mutable List

Supposing we have the following piece of code, in line1 we create list1 and in line2 we create a new variable name list2 and set it to list1. In line 3 we modify list2 and then in line 4 and 5 we print out the original list, list1 and the modified list, list2.

Python
[5, 2, 3, 4]
[5, 2, 3, 4]

When we do this however we see that both list1 and list2 are modified and this is because list2 is list1, both are assigned to the same object in memory.

We can perform some additional checks by using logical operators, to check whether the values of list1 and list2 are equal and whether list1 is list2.

Python
[5, 2, 3, 4]
[5, 2, 3, 4]
True
True

In both cases we see the statement is True. We can also use the function id to look at the memory configuration of list1 and list2:

Python
[5, 2, 3, 4]
[5, 2, 3, 4]
True
2601465937608
2601465937608
True
True

In this case we see the id or memory configuration of list1 and list2 is identical as they are the same object. The last two statements are equivalent to one another, list1 is list2 only if the memory configuration is identical.

If we type in list1 followed by a dot . and then press tab we will see a range of functions which we can use to act on this list. Most of these functions are functions which we can use to change something.

We can modify the code to call list1 and then use the .copy() function which will act on list1 and make a copy. () are in place for input arguments but none are required for this function so the contents are left blank. We can then assign the copied list1 to the variable name list2 (line 2).

Python
[1, 2, 3, 4]
[5, 2, 3, 4]
False
2601466004296
2601465868104
False
False

Now we can see the two lists are not equal to each other and are assigned to a different memory id. We can now repeat the above without the modification on line3.

Python
[1, 2, 3, 4]
[1, 2, 3, 4]
True
2601463458568
2601463726472
False
False

Now we see that list1 and list2 are equal in value but their memory ID is independent.

The line commented out on line3 is a line of code that introduces a single point change to the list or “single-point mutation”. An object that can be changed or “mutated” is called “mutable”.

A Tuple

There are some cases where we want a list that cannot be changed or mutated, this is known as immutable. For example if we are capturing a video file, which is a series of images, that all have the same number of pixels and the same magnification. It may be beneficial for the camera software to assign the number of pixels to a constant or immutable list and the zoom to a constant or immutable list and the video may use this constant or immutable list as part of its code until the stop button is stopped on the camera. Doing so will make it more likely for the software to encode a steady video opposed to a video that is all over the place and a complete mess due to a erroneous mutation introduced in a mutable list.

A list that is immutable is known as a tuple and a tuple cannot be changed or mutated, without being physically deleted. A Tuple can be created by using circular brackets opposed to square brackets.

Python

Although a tuple is created usign circular brackets () for indexing, like a list, square brackets are used.

Python
2

If we have a look at tuple2 by typing in tuple2 dot followed by a tab, we can see the number of functions available compared to a list is substantially smaller. This is because the tuple cannot be mutated and all the functions we used under list with the exception to count and index mutate the list in some way.

Conversion from Tuple to List

It is possible to create a copy of a tuple and assign it to a list by using the function list()

Python

And to create a copy of a list, to a tuple (which can not be modified) by using the function tuple()

Python

Sets

In addition to a list created with square brackets, [ ], a tuple created with circular brackets(), there is a set created with curly brackets {}.

Python

Sets don’t display on the variable explorer (this appears to be an issue with Spyder 3 which is fixed in the beta of Spyder 4) so we need to print them out:

Python
{1, 2, 3, 4, 5}

Note how the set is not listed in the order, originally assigned and the duplicate value is removed. This means we cannot index into a set using square brackets as we don’t know what value is in what position. If we try to we get the following error:

Python
TypeError: 'set' object is not subscriptable

We can type in set1. followed by a tab to show the number of functions available to work on a set1. Unfortunately there seems to be an inconsistency in what Spyder shows if this is done within the editor or within the console… The functions of most interest are those that show up in the console.

We can add an additional value to the list using the function add, here the only input argument is the value to add, as sets don’t have indexes.

Python
{1, 2, 3, 4, 5, 6}

We can use the function remove to remove a value, once again the only input argument is the value to be removed. The function discard appears to carry out the same function as remove.

Python
{1, 2, 3, 4, 6}

The other functions compare two sets.

We can create two sets and use the difference function on set1 with set2 as the input argument, the result will show the values in set1 that are not part of set2

Python
{4, 5}

The function difference_update will once again find the values in set1, not present in set2 but will update set1 to these values:

Python
{4, 5}
{1, 2, 3, 7, 8, 9}

The function intersection will find the common values found in both set1 and set2:

Python
{1, 2, 3}

The function intersection_update will once again find the common values found in set1 and set2 but update set1 to only have these values.

Python
{1, 2, 3}
{1, 2, 3, 7, 8, 9}

The functions issubset or is superset will return a boolean value of true if set1 is a subset (all values of set1 lie within set2) or superset of set2 (all values of set2 lie within set1).

Python
False
False
Python
False
True
True
False
Advertisements