# [ ] review and run example
# the list before delete
sample_list = [11, 21, 13, 14, 51, 161, 117, 181]
print("sample_list before: ", sample_list)
del sample_list[1]
# the list after delete
print("sample_list after: ", sample_list)
sample_list before: [11, 21, 13, 14, 51, 161, 117, 181]
sample_list after: [11, 13, 14, 51, 161, 117, 181]
# [ ] review and run example Multiple Times
# [ ] consider how to reset the list values?
print("sample_list before:", sample_list)
del sample_list[0]
print("sample_list after:", sample_list)
sample_list before: [11, 13, 14, 51, 161, 117, 181]
sample_list after: [13, 14, 51, 161, 117, 181]
# [ ] review and run example
mixed_types = [1, "cat"]
# append number
mixed_types.append(3)
print("mixed_types list: ", mixed_types)
# append string
mixed_types.append("turtle")
print("mixed_types list: ", mixed_types)
mixed_types list: [1, 'cat', 3]
mixed_types list: [1, 'cat', 3, 'turtle']
# [ ] print ft_bones list
# [ ] delete "cuboid" from ft_bones
# [ ] reprint list
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
print(ft_bones)
del ft_bones[2]
print(ft_bones)
['calcaneus', 'talus', 'cuboid', 'navicular', 'lateral cuneiform', 'intermediate cuneiform', 'medial cuneiform']
['calcaneus', 'talus', 'navicular', 'lateral cuneiform', 'intermediate cuneiform', 'medial cuneiform']
# [ ] print ft_bones list
# [ ] delete "cuboid" from ft_bones
# [ ] delete "navicular" from list
# [ ] reprint list
# [ ] check for deletion of "cuboid" and "navicular"
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
print(ft_bones)
del ft_bones[2]
del ft_bones[3]
print(ft_bones)
if ft_bones[2] == "cuboid":
print("3rd item not deleted")
else:
print("3rd item deleted")
['calcaneus', 'talus', 'cuboid', 'navicular', 'lateral cuneiform', 'intermediate cuneiform', 'medial cuneiform']
['calcaneus', 'talus', 'navicular', 'intermediate cuneiform', 'medial cuneiform']
3rd item deleted
# [ ] review and run example
# pop() gets the last item by default
party_list = ["Joana", "Alton", "Tobias"]
print(party_list)
print("Hello,", party_list.pop())
print("\n", party_list)
print("Hello,", party_list.pop())
print("\n", party_list)
print("Hello,", party_list.pop())
print("\n", party_list)
['Joana', 'Alton', 'Tobias']
Hello, Tobias
['Joana', 'Alton']
Hello, Alton
['Joana']
Hello, Joana
[]
# [ ] review and run example
# can pop specific index like pop(3)
number_list = [11, 21, 13, 14, 51, 161, 117, 181]
print("before:", number_list)
number_list.pop(3)
print("after :", number_list)
before: [11, 21, 13, 14, 51, 161, 117, 181]
after : [11, 21, 13, 51, 161, 117, 181]
# [ ] review and run example
# set a variable to a poped value
number_list = [11, 21, 13, 14, 51, 161, 117, 181]
print("list before:", number_list)
num_1 = number_list.pop()
num_2 = number_list.pop()
print("list after :", number_list)
print("add the popped values:", num_1, "+", num_2, "=", num_1 + num_2)
list before: [11, 21, 13, 14, 51, 161, 117, 181]
list after : [11, 21, 13, 14, 51, 161]
add the popped values: 181 + 117 = 298
# [ ] pop() and print the first and last items from the ft_bones list
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
# [ ] print the remaining list
print(ft_bones.pop(0))
print(ft_bones.pop())
print(ft_bones)
calcaneus
medial cuneiform
['talus', 'cuboid', 'navicular', 'lateral cuneiform', 'intermediate cuneiform']
dog_types = ["Lab", "Pug", "Poodle"]
while dog_types:
print(dog_types.pop())
Poodle
Pug
Lab
#[ ] complete the Register Input task above
purchase_amounts = []
while True:
item_price = input("Enter the price of an item then type done when finished: ")
if item_price != 'done':
purchase_amounts += item_price
else:
print(purchase_amounts)
break
['1', '5']
# [ ] complete the Register Total task above
purchase_amounts = []
while True:
item_price = input("Enter the price of an item then type done when finished: ")
if item_price != 'done':
purchase_amounts += item_price
else:
print(purchase_amounts)
break
subtotal = 0
while purchase_amounts != []:
float_price = float(purchase_amounts.pop())
subtotal += float_price
print(subtotal)
['1', '5']
6.0
# [ ] review and run example
dog_types = ["Lab", "Pug", "Poodle"]
if "Pug" in dog_types:
dog_types.remove("Pug")
else:
print("no Pug found")
print(dog_types)
['Lab', 'Poodle']
# [ ] review and run example
dogs = ["Lab", "Pug", "Poodle", "Poodle", "Pug", "Poodle"]
print(dogs)
while "Poodle" in dogs:
dogs.remove("Poodle")
print(dogs)
['Lab', 'Pug', 'Poodle', 'Poodle', 'Pug', 'Poodle']
['Lab', 'Pug', 'Poodle', 'Pug', 'Poodle']
['Lab', 'Pug', 'Pug', 'Poodle']
['Lab', 'Pug', 'Pug']
# [ ] review and run example
# Change to "Lab", etc... to fix error
dogs.remove("Collie")
print(dogs)
Execution Error
ValueError: list.remove(x): x not in list
# [ ] remove one "Poodle" from the list: dogs , or print "no Poodle found"
# [ ] print list before and after
dogs = ["Lab", "Pug", "Poodle", "Poodle", "Pug", "Poodle"]
if "Poodle" in dogs:
dogs.remove("Poodle")
print(dogs)
else:
print("no Poodle found")
['Lab', 'Pug', 'Poodle', 'Pug', 'Poodle']