Key Ideas

  • List Methods
    • append(item)
    • insert(position, item)
    • pop() or pop(position)
    • sort()
    • reverse()
    • index(item)
    • count(item)
    • remove(item)
  • List Append vs. List Concatenation

  • for loop list traversal
    • by element
    • by position
  • A function parameter can be a list

  • A pure function does not modify its parameters. In general, modifying a parameter can produce an undesirable side effect and should be avoided.

  • A function can return a list

  • List Comprehensions (this is a shortcut and is optional to understand)
    • [<expression> for <item> in <sequence>]
    • [<expression> for <item> in <sequence> if <condition>]

Odd Numbers Problem Write a function that counts the number of odd numbers in a list.

#num_odds.py
def num_odds(numbers):
    odds = 0
    for number in numbers:
        if number % 2 == 1:
            odds += 1

    return odds

def num_odds2(numbers):
    return len([True for item in numbers if item % 2 == 1])     # using a list comprehension

numbers = [45, 24, 13, 18, 56, 73, 91]

Append Clone Problem

Write a function that clones the .append() list method without using any list methods.

def append_clone(collection, value):
    return collection + [value]


names = ["Brady", "Jacob", "Quinn", "Tjabe", "Aubrey", "Sarah", "Luke", "Anya", "Maggie", "Eliana", "Addy"]
print(append_clone(names, "Seamus"))

Remove Method Clone

Write a function that clones the .remove() list method without using any list methods.

def remove_clone(collection, value):
    for i in range(len(collection)):
        if collection[i] == value:
            return collection[:i] + collection[i+1:]

    return collection

names = ["Brady", "Jacob", "Quinn", "Tjabe", "Aubrey", "Sarah", "Luke", "Anya", "Maggie", "Eliana", "Addy"]
print(remove_clone(names, "Leif"))

Pop Clone Problem

Write a function that clones the .pop() list method without using any list methods.

def pop_clone(collection, index):
    # slice index item from collection
    item = collection[index]
    collection = collection[:index] + collection[index + 1:]

    # return index item from collection
    return item, collection             # returns a tuple, i.e. (item, collection)

names = ["Brady", "Jacob", "Quinn", "Tjabe", "Aubrey", "Sarah", "Luke", "Anya", "Maggie", "Eliana", "Addy"]
pop_results = pop_clone(names, 1) # removes "Jacob" from names list

print(pop_results)
print(pop_results[0])   # prints "Jacob"
print(pop_results[1])   # prints slice of names

Insert Clone Problem

Write a function that clones the .insert() list method without using any list methods.

def insert_clone(collection, index, value):
    return collection[:index] + [value] + collection[index:]

names = ["Brady", "Jacob", "Quinn", "Tjabe", "Aubrey", "Sarah", "Luke", "Anya", "Maggie", "Eliana", "Addy"]
names = insert_clone(names, 1, "Seamus") # insert "Seamus" into names before "Jacob"

print(names)

Reverse Clone Problem

Write a function that clones the .reverse() list method without using any list methods.

def reverse_clone1(collection):
    new_list = []
    for item in collection:
        new_list = [item] + new_list

    return new_list

def reverse_clone2(collection):
    return collection[::-1]


names = ["Brady", "Jacob", "Quinn", "Tjabe", "Aubrey", "Sarah", "Luke", "Anya", "Maggie", "Eliana", "Addy"]
names = reverse_clone2(names)
print(names)

Count Clone Problem

Write a function that clones the .count() list method without using any list methods.

def count_clone(collection, value):
    num_value = 0
    for item in collection:
        if item == value:
            num_value += 1
    return num_value

names = ["Brady", "Jacob", "Quinn", "Tjabe", "Aubrey", "Sarah", "Luke", "Anya", "Maggie", "Eliana", "Addy", "Maggie]
print(count_clone(names, "Maggie")) # returns 2
print(count_clone(names, "Sarah")) # returns 1

HW:

A. What will the following code output?

girls = ["Aubrey", "Sarah", "Anya", "Maggie", "Eliana", "Addy"]
girls.insert(3, "Beatrice")
print(girls)

B. Finish the command below so the student “Seamus” is added to the list between Quinn and Tjabe.

boys = ["Brady", "Jacob", "Quinn", "Tjabe", "Luke"]
boys.???????

C. For the list below, what command would delete the third item (“Quinn”) from the list?

boys = ["Brady", "Jacob", "Quinn", "Tjabe", "Luke"]