Lists and pop()

Hello, I have just started programming in Python. Could anyone help with a query about pop() and its use in removing an item from a list please.

I can post the code

Thankyou, Si

Not unless you tell us what the query actually is.

Post your question, and a minimal example of code. Otherwise we can’t
help.

Thanks Steven.

The excercise is to print a list of dinner guests, and then remove a guest.

The list of guests prints correctly, but (as I hope you can see) I can’t remove the last guest:

This is the guest list and each guest has an invitation. I have to remove the last guest and print a message apologising that I have to recind the invitation. Hope that makes sense.

dinner_guests = ['field marshall rommel', 'baron von richtofen', 'Lt. joe kenda', 'sir david 
attenborough', 'alan turing', 'friedrich nietzsche', 'richard trevithick']
message = f"Dear {dinner_guests[0].title()}, please accept this invitation to dinner at 
7pm"
print(message)

removed_guest = dinner_guests.pop()
print(f"I am trying to 'pop' {removed_guest.title()} from the list, but he is still appearing. 
Help?")


Thanks in advance

Are you sure that’s not working? It works for me.

>>> dinner_guests = ['field marshall rommel', 'baron von richtofen', 'Lt. joe kenda', 'sir david attenborough', 'alan turing', 'friedrich nietzsche', 'richard trevithick']
>>> removed_guest = dinner_guests.pop()
>>> print(dinner_guests)
['field marshall rommel', 'baron von richtofen', 'Lt. joe kenda', 
'sir david attenborough', 'alan turing', 'friedrich nietzsche']
>>> print(removed_guest)
richard trevithick

What gives you the idea that its not working?

Hi Steven, thanks for replying. Although I removed ‘Richard Trevithic’ with pop() he is still getting an invitation

This is what my program is returning:

“Dear Field Marshall Rommel, please accept this invitation to dinner at 7pm
Your Lordship Baron Von Richtofen, please accept this invitation to dinner at 7pm
Hi Lt. Joe Kenda, please accept this invitation to dinner at 7pm
Dear Sir David Attenborough, please accept this invitation to dinner at 7pm
Dear Alan Turing, please accept this invitation to dinner at 7pm
Dear Friedrich Nietzsche, please accept this invitation to dinner at 7pm
Dear Richard Trevithick, please accept this invitation to dinner at 7pm
I am trying to ‘pop’ Richard Trevithick from the list, but he is still appearing. Help?”

I am using Sublime Text to execute the program. Could that be anything to do with it? I tried running the program from Python in a terminal session, but the result was the same, unless I’ve done something wrong, can’t quite see what though unfortunately.

Thanks for your help so far

Si

Hi Simon,

The code you showed us works correctly. It correctly removes the name
from the list.

If you are running different code, we cannot tell you what it is doing
wrong because we don’t know what that code is. We would have to guess.

My guess is, perhaps you are printing the invitation first, before you
remove the name from the list? But that’s a guess. Without seeing the
actual code you are running, I cannot be certain.

To answer your question about Sublime Text, no, the Python interpreter
doesn’t know and doesn’t care what editor you use, so long as you have
valid Python code in a .py file.

Thanks for the quick reply Steven, sorry for the delay in getting back.

I agree with what you have suggested, I am happy that I am at least coding the program correctly, so I will go with that.

The book I am learning from doesn’t have answers to any of the exercises, nor does it show what the resulting program should look like. It is the Eric Matthes Python Crash Course 2nd edition, which I am enjoying but this aspect is slightly frustrating. I don’t know if you’ve come across books and / or courses online that are better in this respect?

Thankyou for your assistance

Be lucky
Simon