['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:
make_even
integers
Name your file make_even.py
and submit to Gradescope.
Let’s visualize this on Python Tutor
If we remove the return
statement on line 6, will the function still work properly?
A variable doesn’t store values, it stores a reference to an object that lives in your computer memory (RAM)
If you assign a value to an existing object, the variable references that object
Strings are not mutable
title = "Dr."
last_name = "Brown"
print(title + " " + last_name)
title = "Ms."
print(title + " " + last_name)
name = last_name # both point to the same object
last_name = "Silva" # a new object created, last_name point to the new
print(title + " " + name) # name still point to the original string
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 # both point to the same object
names[1] = "Silva" # the object been mutated, no new object created
print(names_copy[0] + " " + names_copy[1]) # both point to the mutated object
Dr. Brown
Ms. Brown
Ms. Silva
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
Here’s how .pop()
works:
['Lavender Haze', 'Calm Down', 'As It Was', 'Her']
.pop()
items using while
loopsongs = ["Lavender Haze", "Calm Down", "As It Was", "Her"]
index = 0
while index < len(songs):
songs.pop(index)
print(songs)
[]
Why we don’t update index in the while loop?
The first function returns the original argument list by removing vowels.
The second function returns a string without vowels.
Note: list is mutable, but string is not.
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
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', 'Her']
It returns a list of integers that represent the indices of the vowels in the original list.
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.
It returns a new list with the items of the original list inverted.
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():
strings = ["banana", "apple", "grape"]
assert reverse_list(strings) == ["grape", "apple", "banana"]
assert strings == ["banana", "apple", "grape"]
print("Passed test")
main()
Passed test
You have 10 minutes to complete the quiz.
No need to write main()
function.