The pop, append and extend methods in lists
Q1. Enter the following code into Python and get it working.
monsters=['goblin', 'orc', 'evil pixie', 'ogre']
extra_evil=['green wizard', 'witch', 'zombie']
bad=['firedog', 'mummy']
print(monsters)
print(extra_evil)
print(bad)
monsters.append('the thing')
print(monsters)
Q2. Describe what the method 'append' does.
Q3. Add this line to the end of the program:
monsters.pop()
print(monsters)
Q4. Describe what the method 'pop' does.
Q5. Change the line above so it reads:
monsters.pop(2)
print(monsters)
Describe what happened this time.
Q6. Modify the above line so it reads:
temp = monsters.pop(2)
print(monsters)
print(temp)
Now describe fully what the method 'pop' does.
Q7. Append the extra_evil list to the monsters list using this line at the end of the code you have written so far:
monsters.append(extra_evil)
print(monsters)
print('The list monsters has',len(monsters),'elements in it.')
Q8. Explain why the list has 5 elements in it.
Q9. Now extend the bad list to the monsters list using this line at the end of the code you have written so far:
monsters.extend(bad)
print(monsters)
print('The list monsters has',len(monsters),'elements in it.')
Explain why the list now has 7 elements in it.
Q10. Clearly explain the difference between the append and extend methods.