more on lists (class slides)

CSc 110 – More lists

Combining lists

What will happen? Run this code on your computer and on Python Tutor

vegetables = ["kale", "carrot", "peas", "celery"]
fruits = ["banana", "tomato", "pear"]
vegetables + fruits

What will happen below? Will vegetables be mutated?

def combine_lists(list_1, list_2):
  list_1 = list_1 + list_2
  return list_1
  
if __name__ == "__main__":
  vegetables = ["kale", "carrot", "peas", "celery"]
  fruits = ["banana", "tomato", "pear"]
  combine_lists(vegetables, fruits)
  vegetables

Combining lists

What will happen? Run this code on your computer and on Python Tutor the difference between the two chunks of code (the one above and the one below)?

def combine_lists(list_1, list_2):
  for item in list_2:
    list_1.append(item)
  return list_1
  
if __name__ == "__main__":
  vegetables = ["kale", "carrot", "peas", "celery"]
  fruits = ["banana", "tomato", "pear"]
  combine_lists(vegetables, fruits)
  print(vegetables)
['kale', 'carrot', 'peas', 'celery', 'banana', 'tomato', 'pear']

Combining lists

What will happen? Run this code on your computer and on Python Tutor the difference between the two chunks of code (the one above and the one below)?

def combine_lists(list_1, list_2):
  list_1.extend(list_2)
  return list_1
  
if __name__ == "__main__":
  vegetables = ["kale", "carrot", "peas", "celery"]
  fruits = ["banana", "tomato", "pear"]
  combine_lists(vegetables, fruits)
  print(vegetables)
['kale', 'carrot', 'peas', 'celery', 'banana', 'tomato', 'pear']

list methods

  • .append(value)
  • .insert(index, value)
  • .pop(index)
  • .remove(value)
  • .extend(list)

Evaluate the code

Indicate when an error occurs.

numbers = [1, 2, 31, 0, 2]
numbers.append(40)
numbers # evaluate this at this point

numbers.insert(3, 10)
numbers # evaluate this at this point

numbers.remove(2)
numbers # evaluate this at this point

numbers.pop(2)
numbers # evaluate this at this point

new_list = [40, 50, 60]
numbers.extend(new_list)
numbers # evaluate this at this point

Evaluate the code – solution

numbers = [1, 2, 31, 0, 2]
numbers.append(40)
numbers # evaluate this at this point
[1, 2, 31, 0, 2, 40]
numbers.insert(3, 10)
numbers # evaluate this at this point
[1, 2, 31, 10, 0, 2, 40]
numbers.remove(2)
numbers # evaluate this at this point
[1, 31, 10, 0, 2, 40]
numbers.pop(2)
numbers # evaluate this at this point
[1, 31, 0, 2, 40]
new_list = [40, 50, 60]
numbers.extend(new_list)
numbers # evaluate this at this point
[1, 31, 0, 2, 40, 40, 50, 60]

Write a function

  1. Its name is remove_and_append
  2. It has three parameters, all lists: original_list, to_remove_list, to_append_list
  3. It mutates the original_list removing values found in to_remove_list and then appending the values in to_append_list

Test case:

test_list = [1, 2, 3]
remove_and_append(test_list, [2, 3, 4], [10, 11])
assert test_list == [1, 10, 11]

Write a function – solution

def remove_and_append(original_list, to_remove_list, to_append_list):
  for item in to_remove_list:
    if item in original_list:
      original_list.remove(item)
  original_list.extend(to_append_list)
  return original_list

if __name__ == "__main__":
  test_list = [1, 2, 3]
  remove_and_append(test_list, [2, 3, 4], [10, 11])
  assert test_list == [1, 10, 11]
  print(test_list) # [1, 10, 11]
[1, 10, 11]

Write python code

Write python code that determines if a number is prime or not.

Write python code

A number n is NOT prime if it is divisible by any number between 2 and n / 2.

The integer 1 is not a prime number.

Your code should have different functions to deal with lists of numbers in different ways (see test cases on next slide)

Write python code

Test cases:

if __name__ == "__main__":
    original_list = [1, 2, 453, 3, 32, 5, 7]
    result = create_dictionary(original_list)
    assert result == {1: False, 2: True, 453: False, 3: True, 32: False, 5: True, 7: True}

    original_list = [102, 250, 509, 345, 2, 4, 547, 563]
    result = create_dictionary(original_list)
    assert result == {102: False, 250: False, 509: True, 345: False, 2: True,
                      4: False, 547: True, 563: True}

    original_list = [102, 250, 509, 345, 2, 4, 547, 563]
    mutate_list(original_list)
    assert original_list == [False, False, True, False, True, False, True, True]

    original_list = [102, 250, 509, 345, 2, 4, 547, 563]
    remove_primes(original_list)
    assert original_list == [102, 250, 345, 4]

Attendance

Submit your code to gradescope for attendance. Name your file prime_numbers.py