SK Singh
SK Singh

Reputation: 35

how can I write a if condition to break out of for loop in Python

    for method,proprties,body in message:

        logger.info(proprties)
        logger.info(method)
        logger.info(body)

        self.channel.basic_ack(proprties.timestamp)
        k=proprties.timestamp
        logger.info(k)
#k = 1463761209429 
#k = 1463761194158  
#k = 1463756480546  

I want to come out of this for loop if k contains 1463761209, I don't care about the last three digits just match me the first 10 digit and come out.

# Escape out of the loop 
        if k == ??:
            break

I want to write a if condition to break out of loop

Upvotes: 1

Views: 2605

Answers (2)

Jay Patel
Jay Patel

Reputation: 560

You can try converting k to a string and check sub string using in operator.

#for-loop
    if '1463761209' in str(k):
        break

That is only if performance is not a big issue!

Upvotes: 1

Rahn
Rahn

Reputation: 5405

if str(k).startswith("1463761209"):
    break

Upvotes: 3

Related Questions