Popping items from a list using a loop in Python [duplicate]

2024/10/1 1:23:22

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 dinnerI am sorry frank I can no longer invite you to dinnerI 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?

Answer

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") 
https://en.xdnf.cn/q/71016.html

Related Q&A

Django Admin Media prefix URL issue

i ve the following folder structuresrc\BAT\templates\admin\base.html src\BAT\media\base.css src\BAT\media\admin-media\base.csssettings.pyMEDIA_ROOT = os.path.join( APP_DIR, media ) MEDIA_URL = /media/ …

lazy processpoolexecutor in Python?

I have a large number of tasks that I want to execute and make the results available via a generator. However, using a ProcessPoolExecutor and as_completed will evaluate the results greedily and store …

error occurs when installing cryptography for scrapy in virtualenv on OS X [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Can Python do DI seamlessly without relying on a service locator?

Im coming from the C# world, so my views may be a little skewed. Im looking to do DI in Python, however Im noticing a trend with libraries where they all appear to rely on a service locator. That is, y…

Producing pdf report from python with bullet points

I would like to produce a dynamic pdf document from a python script that looks like the image below. Each sentence starts with a bullet point, and the text and number of lines depends on what the user …

Converting bits to bytes in Python

I am trying to convert a bit string into a byte string, in Python 3.x. In each byte, bits are filled from high order to low order. The last byte is filled with zeros if necessary. The bit string is ini…

Install python package from private pypiserver

I have setup a pypiserver behind an nginx proxy which uses htpasswd for authentication. I am currently able to upload sdists, but I cant figure out how to download them. I want to be able to download t…

Matplotlib Table- Assign different text alignments to different columns

I am creating a two column table and want the text to be as close as possible. How can I specify that the first column be right aligned and the second be left aligned?Ive tried by setting the general …

How would I make a random hexdigit code generator using .join and for loops?

I am new to programming and one assignment I have to do is create a random hexdigit colour code generator using for loops and .join. Is my program below even close to how you do it, or is it completely…

Multiline python regex

I have a file structured like this : A: some text B: more text even more text on several lines A: and we start again B: more text more multiline textIm trying to find the regex that will split my file …