Lists: some other list operations - SOLVED#

Removing items from lists#

You can also remove values from a list. Let’s say that we want to remove the 4th entry (x=4) from our list of powers of 4 above. So that the list would contain (\(4^1\), \(4^2\), \(4^3\), \(4^5\), \(4^6\), \(4^7\).) There’s a few ways to do this. Let’s illustrate:

pow_list = [4, 16, 64, 256, 1024, 4096, 16384]

pow_list_copy = pow_list

# There's a couple of ways to remove items from the list "in place".
# This means that the original list is destroyed and only the new list remains

# This way removes the element at position 3 (the fourth element remember, as we count from zero!) The number
# removed can be saved to a variable
removed_value = pow_list.pop(3)
print("Using .pop method")
print(removed_value)
print(pow_list)
# Note that the copied list is also changed
print("Copied list", pow_list_copy)
print()
Using .pop method
256
[4, 16, 64, 1024, 4096, 16384]
Copied list [4, 16, 64, 1024, 4096, 16384]
# This is basically the same idea, but here we remove the first number equal to 256.
pow_list = [4, 16, 64, 256, 1024, 4096, 16384]
pow_list_copy = pow_list
# The .remove method does *not* return the removed number
pow_list.remove(256)
print("Using .remove method")
print(pow_list)
# Note that the copied list is also changed
print("Copied list", pow_list_copy)
Using .remove method
[4, 16, 64, 1024, 4096, 16384]
Copied list [4, 16, 64, 1024, 4096, 16384]
# We could also construct a new list, which doesn't contain this value
pow_list = [4, 16, 64, 256, 1024, 4096, 16384]
pow_list_copy = pow_list

pow_list = [pow_list[0], pow_list[1], pow_list[2], pow_list[4], pow_list[5], pow_list[6]]
# NOTE: This above line is a bit tricksy. We are redefining a variable using values from the original variable.
# This is okay but pow_list changes once this is evaluated.

print("Redefining the list")
print(pow_list)
print("Copied list, look this hasn't changed!", pow_list_copy)
Redefining the list
[4, 16, 64, 1024, 4096, 16384]
Copied list, look this hasn't changed! [4, 16, 64, 256, 1024, 4096, 16384]
# A shorthand way of doing what was written above is the following:

pow_list = [4, 16, 64, 256, 1024, 4096, 16384]
pow_list_copy = pow_list

pow_list = pow_list[0:3] + pow_list[4:7]
# This introduces the "slicing" syntax. You will need to be familiar with this for later classes, don't worry, we'll
# give lots of practice in future classes!

# Here pow_list[0:3] means all elements of pow_list between 0 and 2. The first element *is* included, the last
# element is *not* included. That's a bit weird, again, more practice will come!

# Finally as pow_list[0:3] and pow_list[4:7] are both lists, we join them together with the "+" operator

print("Redefining the list")
print(pow_list)
print("Copied list, look this hasn't changed!", pow_list_copy)
Redefining the list
[4, 16, 64, 1024, 4096, 16384]
Copied list, look this hasn't changed! [4, 16, 64, 256, 1024, 4096, 16384]

EXERCISE#

    1. Create a list of \(x^{1./4.}\) for x between and including x=20 and x=120.

    1. Change the 20th, 40th, 60th and 80th elements to be 1.

    1. Remove the 10th, 30th, 50th, 70th and 90th elements.

    1. For the new list (should now be 96 elements long). Change the 15th and 25th elements to 0.

your_list = []

for x in range(20,121):
  your_list.append(x**(1/4))

for i in [19,39,59,79]:
  your_list[i] = 1

#remove the 10th, 30th, 50th, 70th and 90th elements
#doing this backwards ensures we don't have to play about with re-indexing
your_list.pop(89)
your_list.pop(69)
your_list.pop(49)
your_list.pop(29)
your_list.pop(9)

print(len(your_list))

for j in [14,24]:
  your_list[j] = 0
96
expected_answer = [2.114742526881128, 2.1406951429280725, 2.1657367706679937, 2.189938703094842, 2.213363839400643, 2.23606797749979, 2.2581008643532257, 2.2795070569547775, 2.300326633791206, 2.340347319320716, 2.359611061770567, 2.378414230005442, 2.39678172692843, 2.414736402766418, 0, 2.449489742783178, 2.4663257145596607, 2.4828237961983883, 1, 2.514866859365871, 2.530439534435243, 2.5457298950218306, 2.5607496020310148, 2.5755095769013945, 0, 2.604290687140218, 2.6183304986958853, 2.6321480259049848, 2.6591479484724942, 2.6723451177837885, 2.68534961428265, 2.6981678764080854, 2.7108060108295344, 2.7232698153315003, 2.7355647997347607, 2.7476962050544724, 2.7596690210718946, 1, 2.7831576837137404, 2.794682392671241, 2.806066263296683, 2.8173132472612576, 2.8284271247461903, 2.8394115144336776, 2.8502698827717983, 2.861005552576305, 2.8716217110259006, 2.892507608519078, 2.9027831081870996, 2.9129506302439405, 2.923012785691765, 2.932972087668518, 2.942830956382712, 2.9525917237371893, 2.962256637665299, 2.9718278662008415, 1, 2.990697562442441, 3.0, 3.009216698434564, 3.018349479292333, 3.027400104035091, 3.036370276710811, 3.0452616464756694, 3.0540758099773515, 3.062814313608786, 3.080070288241023, 3.088590619387661, 3.0970410146824725, 3.1054227990714813, 3.11373725847777, 3.121985641352145, 3.1301691601465746, 3.138288992714996, 3.1463462836457885, 1, 3.1622776601683795, 3.1701538797227005, 3.1779718278112656, 3.1857325005549697, 3.19343686757474, 3.2010858729436795, 3.208680436096278, 3.216221452697031, 3.2237097954706257, 3.238531840464366, 3.245867180408456, 3.2531531233955713, 3.260390438695134, 3.2675798769167543, 3.2747221706220526, 3.281818034911291, 3.288868167986058, 3.295873251689181, 3.3028339520229766, 3.309750919646873]

# Make sure your list from above is called your_list!!!

max_difference = 0
for i in range(len(your_list)):
    current_diff = abs(expected_answer[i] - your_list[i])
    if current_diff > max_difference:
        max_difference = current_diff

print("Maximum difference. This should be 0 (but might be almost 0 due to rounding error):", max_difference)
Maximum difference. This should be 0 (but might be almost 0 due to rounding error): 0

Summing over a list#

We’ve already shown above how we can use for loops to sum over lists. However, this can also be done using the builtin sum function in python. Here’s how this works

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

# Summation using a for loop

total = 0
for item in med_list:
    total = total + item

print(total)

# Direct summation using `sum`
print(sum(med_list))
107
107

Adding two lists together.#

To combine (concatenate) two lists together we can use the addition operator.

L1 = [4,7,8]
L2 = [7,3,1]

L3 = L1 + L2

print (L1, L2, L3)
[4, 7, 8] [7, 3, 1] [4, 7, 8, 7, 3, 1]
L1 = [4,7,8]
L2 = [7,3,1]

# To combine these together without creating a new variable we could do
L1.extend(L2)
# But now we no longer have the original list L1

print (L1, L2)
[4, 7, 8, 7, 3, 1] [7, 3, 1]

Quick exercise#

Create two lists

  • list1, to have elements 2,4,6,8,10,

  • list2, to have elements, 1,3,5,7,9 Now concatenate them together that you obtain a list that has elements \(1, 2, 3, 4, 5, 6, 7, 8, 9, 10\).

list1 = [2,4,6,8,10]
list2 = [1,3,5,7,9]

list3 = []

for i in range(5):
  list3.append(list2[i])
  list3.append(list1[i])

print(list3)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#alternative method

list4 = []

for i,j in zip(list1,list2):
  list4.append(j)
  list4.append(i)

print(list4)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]