['Lavender Haze', 'Calm Down', 'As It Was', 'About Damn Time']
Lists are mutable
Strings are not
In addition to retrieving an item from a list
, we can remove or change it (which is not something you can do with strings)
['Lavender Haze', 'Calm Down', 'As It Was', 'About Damn Time']
(strings are not)
Because lists are mutable:
once a list is changed inside a function, that change persists when the function has finished running
if your function changes a list, you don’t strictly need to return that list – we will be returning because that’s what you need to be doing in the future
make_all_even
integers
def make_all_even(integers):
index = 0
while index < len(integers):
if integers[index] % 2 == 1:
integers[index] += 1
index += 1
return integers
def main():
test_integers = [1, 2, 3, 4]
assert make_all_even(test_integers) == test_integers
assert test_integers == [2, 2, 4, 4]
print(test_integers) # we print the list we created before function call
main()
[2, 2, 4, 4]
Let’s visualize this on Python Tutor
Strings are not mutable
title = "Dr."
last_name = "Brown"
print(title + " " + last_name)
title = "Ms."
print(title + " " + last_name)
name = last_name
last_name = "Silva" # last_name points to a different object
print(title + " " + name)
Dr. Brown
Ms. Brown
Ms. Brown
Visualize these examples in Python Tutor
Lists are mutable
names = ["Dr.", "Brown"]
print(names[0] + " " + names[1])
names[0] = "Ms."
print(names[0] + " " + names[1])
names_copy = names
names[1] = "Silva"
print(names_copy[0] + " " + names_copy[1])
Dr. Brown
Ms. Brown
Ms. Silva
Visualize these examples in Python Tutor
Visualize these examples in Python Tutor
When working with lists, once they are changed in a function, the changes happen to the object in memory
Changes to lists persist once the function has finished running
.pop()
list methodWe will be using a few built-in list methods
(not all of them, some you will implement yourself from scratch)
Here’s how .pop()
works:
['Lavender Haze', 'Calm Down', 'As It Was', 'About Damn Time']
remove_vowels_list
and remove_vowels_string
characters
as argument, the second takes a single string
.pop(index)
) all vowels from the characters
list, the second creates a new_string
with only characters that are not vowelsnew_string
def remove_vowels_list(characters):
index = 0
while index < len(characters):
if characters[index] in "aeiou":
characters.pop(index)
else:
index += 1 # go to next index only if no item has been removed
return characters
def remove_vowels_string(string):
new_string = ""
index = 0
while index < len(string):
if string[index] not in "aeiou":
new_string += string[index]
index += 1
return new_string
def main():
test_characters = ["b", "a", "n", "a", "n", "a"]
test_string = "banana"
assert remove_vowels_list(test_characters) == test_characters
assert test_characters == ["b", "n", "n"]
assert remove_vowels_string("banana") == "bnn"
print(test_characters)
print(test_string)
main()
['b', 'n', 'n']
banana
You have 10 minutes to complete the quiz
main()
, no test casesBuilt-in functions you can use: round()
, input()
, float()
, str()
, int()
, len()
— you don’t have to use all of these
We will be using the following list methods in this class:
.append()
adds an element at the end of the list: list.append(value)
.insert()
adds an element at the provided index: list.insert(index, value)
.pop()
removes a specific element at the provided index: list.pop(index)
.remove()
removes the first element with the provided value: list.remove(value)
.append()
list method['Lavender Haze', 'Calm Down', 'As It Was', 'About Damn Time']
indices_of_vowels
string
as its parameter.Test cases:
def indices_of_vowels(string):
result = [] # initialize empty list to hold indices
index = 0 # initialize index
while index < len(string):
if string[index] in "aeiou": # check if character is vowel
result.append(index) # append index to result
index += 1 # increment index
return result
def main():
assert indices_of_vowels("hello") == [1, 4]
assert indices_of_vowels("") == []
assert indices_of_vowels("aeiou") == [0, 1, 2, 3, 4]
print("Passed all tests.")
main()
Passed all tests.
reverse_list
Test case:
def reverse_list(items):
index = len(items) - 1 # initialize index
inverted_list = []
while index >= 0:
inverted_list.append(items[index])
index -= 1
return inverted_list
def main():
test_strings = ["banana", "apple", "grape"]
assert reverse_list(test_strings) == ["grape", "apple", "banana"]
print("Passed test")
main()
Passed test