Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 567015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:02:18+00:00 2026-05-13T13:02:18+00:00

I have this piece of code (should be self-explanatory; if not, just ask): for

  • 0

I have this piece of code (should be self-explanatory; if not, just ask):

for tr in completed_taskrevs:
    found = False
    for nr in completion_noterevs:
        if tr.description in nr.body:
            completion_noterevs.remove(nr)
            found = True
            break
    assert found

How can I make it more pythonic?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:02:18+00:00Added an answer on May 13, 2026 at 1:02 pm

    Using assert/AssertionError is probably wrong here, I’d say. It’s useful for “debugging assertions”, which is to say, to make sure your code is sane. It’s not useful as a way to raise an error when you get invalid data, for many reasons, the most interesting of which is probably that assert isn’t even guaranteed to be executed– if your code is compiled with any optimization settings, it won’t be. And heck, even if this is a debugging thing, I’d still use raise– it’s more readable, and it’ll always happen, no matter when or why the data is wrong.

    So to make it more “pythonic”, I would remove the assert and replace it with something nicer. As it happens, such a nicer thing exists, and it’s the raise statement. Further, I would replace the clumsy value set/check with the else clause of loops, which is executed when the loop is exhausted (or, for while loops, when the condition becomes false). So if you break, the else clause is not executed.

    for tr in completed_taskrevs:
        for nr in completion_noterevs:
            if tr.description in nr.body:
                completion_noterevs.remove(nr)
                break
        else:
            raise ValueError("description not found"); # or whatever exception would be appropriate
    

    Other than this, I probably wouldn’t change anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program that should execute a piece of code base on the
I have got a piece of code, that should countdown some number (in this
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code: window.addEventListener( focus , function(){ window.console.log(focus); } , false
I have this piece of code. It should implement my custom validations, but it
I have this piece of code that works fine in subsonic 2.2, I migrated
I have this piece of code in C++: ihi = y[0]>y[1] ? (inhi=1,0) :
I have this piece of code: <li class=selected><a href=/AmIACandidate.html>Am I a Candidate?</a></li> I need
I have this piece of code in javascript: var catId = $.getURLParam(category); In my
I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.