Indices and ordering: INTERACTIVE - SOLVED#

Lists are ordered, this simply means that you have “the first object in the list”, “the second object in the list”, etc. An index is a position of an element in the list. In python, the first element of a list has index \(0\) not \(1\), therefore indices start from \(0\). REMEMBER THIS in python we count from 0 … This is true in most computer languages, but as we will see in TB2, it is not true in matlab.

We can access elements of the list below (pow_list) in the following way: If we want the second element we do pow_list[1]. SAYING THIS AGAIN python counts from 0, so the first element of the list (the 1) is at position “0”, the second element (the 4) at position “1” and so on.

pow_list = [1, 4, 9, 16, 25, 36]
print(pow_list[1])
4

EXERCISE#

Print the 1st, 4th and 7th elements of this list

print(pow_list[0],pow_list[2])
1 9

If you try to access an element of the list that is out of bounds the code will fail.

EXERCISE#

Try to access the 8th element of this list

However, there is a strange (but sometimes useful) feature that you can index on negative numbers. For example pow_list[-1] will access the last element of the list, pow_list[-2] the second to last element and so on. pow_list[-8] will still fail.

pow_list[-1]
36

Changing elements#

Lists are a “mutable” type. This means that we can change the values of the elements in the list at will. For example let’s change the second value of this list to 15.

# Setting the list again explicitly here in case there were issues above
pow_list = [1, 4, 9, 16, 25, 36]
pow_list[1] = 15
print(pow_list)
[1, 15, 9, 16, 25, 36]

We can change the value back, and again see how this changes

pow_list[1] = 4
print(pow_list)
[1, 4, 9, 16, 25, 36]

You can see that the value has changed within the list. If you were to send this list to a function, and the function changes the value, the same thing happens:

def change_value_1(input_list):
    input_list[1] = 15
    return input_list

pow_list = [1, 4, 9, 16, 25, 36]
change_value_1(pow_list)
print(pow_list)
[1, 15, 9, 16, 25, 36]

BUT BE CAREFUL HERE if you assign a list to variables, and change a value in one, it changes both, as both variables reference the same object. If you want to make a copy of the list you have to create a new object and fill it with the same values.

def change_value_1(input_list):
    input_list[1] = 15
    return input_list

pow_list = [1, 4, 9, 16, 25, 36]
# Let's copy the list badly!
backed_up_pow_list = pow_list
# Now we copy it properly!
properly_backed_up_pow_list = []
for entry in pow_list:
    properly_backed_up_pow_list.append(entry)

# Now change a value in the original list.
change_value_1(pow_list)
print("Here is the list we changed")
print(pow_list)
print()

# Oh no this changed too!
print("Here is the copied list, which has also changed!! Be careful!")
print(backed_up_pow_list)
print()

# But this does not change
print("Here is the correctly copied list, this has not changed.")
print(properly_backed_up_pow_list)
print()
Here is the list we changed
[1, 15, 9, 16, 25, 36]

Here is the copied list, which has also changed!! Be careful!
[1, 15, 9, 16, 25, 36]

Here is the correctly copied list, this has not changed.
[1, 4, 9, 16, 25, 36]

Iterating over lists#

Given a list:

med_list = [1, 4, 5, 6, 2, 9, 10, 16, 38, 16]

we may iterate over its elements, say we wanted to print each of the elements:

N = len(med_list)

for k in range(N):
    print(med_list[k], end = " ")
1 4 5 6 2 9 10 16 38 16 

BUT it is much easier to do this by lopping over the items in the list directly, and it is more pythonic.

for item in med_list:
    print(item, end=" ") # Note the `end = " "` makes sure everything is printed on a single line!
1 4 5 6 2 9 10 16 38 16 

The range object is something clever (for a later class!) but it can be converted to a simple list by doing:

list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

This lets us create easy lists of consecutive numbers!

Exercise#

Create a list of the first 7 powers of 4 (\(4^1\), \(4^2\), .. \(4^7\).). Hint: remember the ‘append’ to list function.

This list should be [4, 16, 64, 256, 1024, 4096, 16384]

my_lst = []

for k in range(1,8):
  my_lst.append(4**k)

print(my_lst, end=" ")
[4, 16, 64, 256, 1024, 4096, 16384] 

Exercise#

    1. Create a list of \(x + 3 x^2 + 5 x^5\) for x between 1 and 7. E.g. [(1 + 3* 1**2 + 5 * 1**5), (2 + 3* 2**2 + 5 * 2**5), ...]

    1. Change all values of the list where x is even (the 2nd, 4th and 6th values) to be \(-x + 3 x^2 - 5 x^5\)

  • Your list should now look like [9, -150, 1245, -5076, 15705, -38778, 84189].

new_list = []

for x in range(1,8):
  new_list.append(x + 3*x**2 + 5*x**5)

print(new_list)

i = 0
for item in new_list:
  if item % 2 == 0:
    x = i+1
    new_list[i] = -1*x + 3*x**2 - 5*x**5
  i += 1

print(new_list)
[9, 174, 1245, 5172, 15705, 38994, 84189]
[9, -150, 1245, -5076, 15705, -38778, 84189]