2026fa_cse160/lecture20261110/listmethods/list09MethodPop.py

40 lines
1,018 B
Python

#!/usr/bin/python3
# Program: list09MethodPop.py
programMessage = '''
This program popss a value from a list. The value is removed from
the end of the list or at the position specified by an optional index.
'''
print(programMessage)
# Lists of text values
dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Print the list and the length
print("Print the length and type of the list.")
print (dogNames," length = ",len(dogNames),"Type =",type(dogNames))
print("\nPrint each element of the list and it's index")
index = 0
for item in dogNames:
print(f"dogNames[{index}] = {item}")
index = index + 1
print(f"Pop last element")
dog = dogNames.pop()
print(f"Removed {dog} from {dogNames}")
print(f"Pop last element")
dog = dogNames.pop()
print(f"Removed {dog} from {dogNames}")
print(f"Pop last element")
dog = dogNames.pop()
print(f"Removed {dog} from {dogNames}")
print(f"Pop element at position 1")
dog = dogNames.pop(1)
print(f"Removed {dog} from {dogNames}")