I am new to coding and am working with lists. One of the prompts is asking me to use the .pop() method to remove names from my list. Whenever i use the pop method though, it is not removing the last name on the list (Aragorn) which was appended. It is instead removing the name (Pippin) which is right before Aragorn in the list. I am not sure what i did wrong for this to happen.
#π .pop() not working like i thought
17 messages Β· Page 1 of 1 (latest)
@mystic wedge
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
send code as text please
!code
guests = ['Boromir', 'Merry', 'Pippin']
invite = 'please come to dinner.'
print(f"{guests[0]}, {invite}")
print(f"{guests[1]}, {invite}")
print(f"{guests[2]}, {invite}")
print(f'\nSorry, {guests[0]} cannot make it to dinner.')
#boromir couldnt make it, replace with frodo
guests[0] = 'Frodo'
print('\nWe found a bigger table and are able to invite more guests.')
guests.insert(0, 'Sam')
guests.insert(2, 'Gimli')
guests.append('Aragorn')
print(f"\n{guests[0]}, {invite}")
print(f"{guests[1]}, {invite}")
print(f"{guests[2]}, {invite}")
print(f"{guests[3]}, {invite}")
print(f"{guests[4]}, {invite}")
print(f"{guests[-1]}, {invite}")
print('\nThe large table will not be ready in time, only two people can be invited.')
uninvite = 'sorry we could not invite you.'
guests.pop()
popped_guests = guests.pop()
print(f"\n{popped_guests}, {uninvite}")
Hey @mystic wedge!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
you call pop twice
but you only store the value once
it's removing the name 'Pippin' when you pop it the first time and you never do anything with that value so it falls into the void
printing the list on console each time you do something on the list can help you in debugging
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.