Indexing tuples and lists that contain tuples and lists: BEHIND THE SCENES!

Indexing Tuples and Lists that contain Tuples and Lists


Hi! Welcome to this “Indexing tuples and lists” tutorial. We will be covering the principles behind indexing tuples and lists that contain tuples and lists as elements (they can also contain other data types, we’ll see an example of this right away).


Presenting the examples we will be working with!

As promised! Here are the two examples we will be working with during this tutorial. As you can see, we have a list and a tuple but the curious thing about them is that some of their element are also lists and tuples.

NOTE: In these examples lists contain lists and tuples contain tuples, but lists can contain tuples as well and viceversa



Finding an element in this data structure

Let’s analyze the question presented in the slide above by following the diagram below. “How can we find "house3" in List1?
  • We’ll start by breaking down the structure of the outermost list. If we look carefully at the correspondence between square brackets, we’ll notice that the outermost list only contains two elements: a list at index 0 and a string at index 1.
  • We can then break down the first element into its constituent parts:
    • Its first element is a string, its second element is a list and its third element is a string.
  • We can keep breaking down the structure this way until we’ve reached the innermost level of our data structures.

This gives us a path to follow to find an element in the list or tuple.
To find an element we index subsequently following the path down in the diagram until we reach the element we are looking for.

Let’s check it out! : )




Required Syntax

You may be thinking: Wait a minute! What is this double and triple indexing you’ve used in the Python shell image above?
Well, this double indexing is allowing us to reach a specific element in the list, even if that element is inside a list that is itself an element of the outermost list
(Yes, that’s a mouthful hahaha)
  • When using this syntax, the indices are evaluated from left to right.
    1- First we have the variable name (list1), followed by the index of the element in the outermost list we wish to access. That will give us access to the list in the 0th index.
    2- Now that we have this access, we can request any of the elements it directly contains.
In this case, we request for the element at index 1 and so we get access to the list at index 1 of this inner list which is itself another list.

Check the diagram above if you feel like you need to go over this structure again : )


"house5" not in list1?

Now, you may find interesting that when we check if house3 is in list1, the value returned is False.
You may think: “Why is it False? It is contained in list1”. The problem is that “house3” is not directly contained within list1, it is contained in a list that is contained in a list that is itself contained within the outermost list.
In contrast, “house5” is directly contained within list1 (at index 1), so checking if it is in list1 returns True (as you can see in the Shell image below).



Practice time! :-)

Now it’s time for you to practice! I’ve made an example of a tuple that contains tuples. Work through this problem, make a diagram like the one shown above to determine the structure of the tuples and try to answer these questions.




The image below is the diagram of the tuple’s structure to help you check your understanding and as always, you can check your answers in Python’s Shell.



Hope it helps!
If you have any questions, please do not hesitate to post them in the forums. Your classmates and TAs will be there to help you : )
Estefania.

Comments