Lucas Johns
Lucas Johns

Reputation: 83

Popping items from a list using a loop in Python

I'm trying to write a for loop in python to pop out all the items in a list but two, so I tried this:

guest = ['john', 'phil', 'andy', 'mark', 'frank', 'joe']

for people in guest:
    popped_guest = guest.pop()
    print("I am sorry " + popped_guest + " I can no longer invite you to dinner")

And, this is what I get when I run it:

I am sorry joe I can no longer invite you to dinner

I am sorry frank I can no longer invite you to dinner

I am sorry mark I can no longer invite you to dinner

So, it only pops the 3 but is there a way to get it to pop 4 of the 6? I tried adding an if statement:

guest = ['john', 'phil', 'andy', 'mark', 'frank', 'joe']

for people in guest:
    if people > guest[1]:
        popped_guest = guest.pop()
        print("I am sorry " + popped_guest + " I can no longer invite you to dinner")

I would have thought since that 'phil' would be 1 that it would pop the last 4 but when I ran the program it returned nothing. So, is it possible to do in one for loop?

Upvotes: 8

Views: 15452

Answers (8)

You can use list() and copy() for numbers to create a different copied numbers as shown below:

guest = ['john', 'phil', 'andy', 'mark', 'frank', 'joe']
             # ↓ Here ↓
for people in list(guest):
    popped_guest = guest.pop()
    print("I am sorry " + popped_guest + " I can no longer invite you to dinner")

Or:

guest = ['john', 'phil', 'andy', 'mark', 'frank', 'joe']
            # ↓ ↓ Here ↓ ↓
for people in guest.copy():
  popped_guest = guest.pop()
  print("I am sorry " + popped_guest + " I can no longer invite you to dinner")

Output:

I am sorry joe I can no longer invite you to dinner
I am sorry frank I can no longer invite you to dinner
I am sorry mark I can no longer invite you to dinner
I am sorry andy I can no longer invite you to dinner
I am sorry phil I can no longer invite you to dinner
I am sorry john I can no longer invite you to dinner

Upvotes: 0

Subham
Subham

Reputation: 411

For letting 2 values still in the list. And when list length is unknown.

guest = ['john', 'phil', 'andy']

except2 = (len(guest)) - 2

for i in range(except2):
    popped_guest = guest.pop()
    print("I am sorry " + popped_guest + " I can no longer invite you to dinner") 
    
else:
    for i in guest:
        print(f"You are invited  {i}")
I am sorry andy I can no longer invite you to dinner
You are invited  john
You are invited  phil

[Program finished]

Upvotes: 0

Marc
Marc

Reputation: 304

I am adding some code Hope it can help you out.

text = ["hello", "world"]

while True:
    try:
        val = text.pop()
        print(val)
    except IndexError:
        return

Upvotes: -1

Rolo787
Rolo787

Reputation: 370

Your for loop discontinues after its 3rd iteration since that is how many elements are left in guest after having popped the previous ones. You can potentially use a while loop to continuously pop elements until the list remains with just 2.

while len(guest) > 2:
    popped_guest = guest.pop()
    ...

Upvotes: 11

Himansu Padhee
Himansu Padhee

Reputation: 1

invite = ['Pankaj', 'Deepak', 'Nischey', 'Ram', 'Martin', 'Gopal']

for item in invite:
    if invite.index(item) > 1:
        popped_guest = invite.pop()
        print("Sorry you are not invited " + popped_guest)
    else:
        print("you are invited " + item)

Upvotes: 0

J. Olsson
J. Olsson

Reputation: 98

As mentioned, your code is not doing at all what you think it is, because you are popping elements out of the list while actively iterating through it. I would say "a much better coding practice would be to duplicate the list to pop out of," but it's not a "better" practice - your way simply doesn't work at all how you want it to, it will always pop out the first half of your list.

I would ask myself "How do I specify who gets popped in my current iteration," and "Where do I set how many people get popped in my current iteration". The answer to both questions appears to be "I don't."

Upvotes: 1

Patrick Haugh
Patrick Haugh

Reputation: 60944

If you want to pop 4 things, then just count to 4

for _ in range(4):
    popped_guest = guest.pop()
    print("I am sorry " + popped_guest + " I can no longer invite you to dinner") 

Upvotes: 8

Mia
Mia

Reputation: 2676

The reason for this behavior is that when you used a for loop in Python, it actually go through the list by its index number. Therefore, when you are looping over a list and mutating it at the same time, you may get a few elements skipped. A better way is to mutate a copy of the list while looping over the original one.

Upvotes: 0

Related Questions