ubuntu2004
Loops IV - looping by index
We have so far only concerned ourselves with looping over a single list. Now we consider how to loop over multiple lists simultaneously.
But before we do that we need to introduce a new function called range()
.
How range()
works
In its simplest form range(n)
produces a sequence of ascending integers starting from zero and ending at n-1
. The following code shows how it works.
In this example the loop iterates 3 times. The iterating variable is i
. On the first iteration i
is assigned the value 0, on the second iteration i
is assigned the value 1, and on the third and last iteraton i
is assigned the value 2.
Using range(n, m)
produces a sequence of ascending integers starting at n
and ending at m-1
. The following code shows how it works.
Finally, using range(n, m, s)
produces a sequence of ascending integers starting at n
, ending at m-1
and with a step size s
. The following code shows how it works.
Using range()
to loop through strings
In loops range()
is commonly used to access characters in a string using their indices. Remember from Notebook 6 that each character in a string has an index (or position) and we can use that index to access the character from the string. This table shows the indices of the string "Hello, world!" which we saw in Notebook 6.
string: | H | e | l | l | o | , | W | o | r | l | d | ! | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
index: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
The following code prints the indicies of all characters in "Hello, world!" using range()
Notice how in the loop,
we use the function len(sentence)
to get the number of characters in the string "Hello, world!" (13 in total), and then use that length in range()
. Instead we could have written
but that would mean manually counting the number of characters in sentence
first. Why do something when you can get the computer to do it for you?
In the print()
function we also used a tab escape character "\t" to format the output into two columns.
A realistic example of looping through a string: Extracting codons
Consecutive triplets of bases in DNA are called codons. Which are translated into amino acids to produce proteins. Let's write some code to extract codons from a DNA sequence.
In range()
we have set the step size to 3 so that we index every third position in the DNA sequence, i.e., 0, 3, 6, 9, and and so on.
We use string slicing to access substrings (i.e., the codons) of the DNA sequence. For example, when the iterating variable i
is 0 the slice dna_seq[i:i+3]
is dna_seq[0:3]
which accesses the first to third characters in dna_seq
which is "TTA". On the next iteration of the loop i
is 3, so we access the characters dna_seq[3:6]
, i.e., the fourth to sixth characters which are "TGT". And so on.
Using range()
to loop through lists
Using range()
to loop through a list is the same as using it to loop through a string.
Notice how we have used n
(the number of items in the list) in range(n)
.
The only difference in the above code and the code in the Notebook 12 is the loop. In Notebook 12 we had
and in this Notebook we have
Both do exactly the same thing. For the simple code we have written here, the first method is probably preferred because it is easier to read and understand. However, for other tasks the latter method may be better. It all depends on the task.
Simultaneous looping through multiple lists
Say we have two lists, one with forenames and another with surnames. Our task is to loop through both lists simultaneously, concatenate (join) each forename and surname and create a new list with each full name.
For example if we start with two lists
By combining the lists forenames
and surnames
we want to obtain the list
Let's write out the algorithm for doing this
Initialise an empty list called
fullnames
Loop through the lists
forenames
andsurnames
simultaneously so that we have a forename and surname pair.Concatenate forename and surname into a full name.
Append the full name to the list called
fullnames
.
Apart from Step 2 we have seen how to do the other steps in Notebooks 5 (strings) and 11 (lists). So how do we loop through two lists simultaneously?
We can use range()
to loop through the indicies of the lists. This is shown in the following code