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 8912659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:15:33+00:00 2026-06-15T04:15:33+00:00

if a == arg: if arg not in a_list: do_something(arg) elif b == arg:

  • 0
if a == arg:
    if arg not in a_list:
        do_something(arg)
elif b == arg:
    if arg not in b_list:
        do_something(arg)
else:
    do_something_else(arg)

Propose an idiomatic way to shorten and specifically DRY above statements – without extracting parts of it to separate functions.

edit: Funny 😉 This not a homework assignment neither is this a request to review my code, perhaps if I reframe above as below my intentions become more clear:

What is a pythonian way to write above if-statements such that it is both DRY and idiomatic to the language? I feel I am missing something, perhaps one can make use of the and/or operators in python? Reason for not extracting any parts of it to separate functions is that then this question is more about refactoring than about idiomatic pythonian if-logic.

  • 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-06-15T04:15:34+00:00Added an answer on June 15, 2026 at 4:15 am

    Here is a simple answer:

    for i, i_list  in [(a, a_list), (b, b_list)]:
        if i == arg:
            if arg not in i_list:
                do_something(arg)
            break
    else:
        do_something_else(arg)
    

    Combining the a, and a_list and b, and b_list into tuples, we can easier iterate them.
    Then its a simple matter of the iteration itself, matching the conditions, and running the desired function..

    Python supports for....else statements.

    The else statement will run if there are no returns or breaks in the for loop.

    Here, if we get our desired match, the loop will break, and we are done. if we never break, then we get to our else loop. In my opinion this is the most Pythonic way to do this.

    Edit

    To make this any better, you should have a, b, a_list, b_list inside some kind of dictionary, where d[a] = a_list

    Then you could just iterate over the dict like this:

    for k, v  in d.items():
        if k == arg:
            if arg not in v:
                do_something(arg)
            break
    else:
        do_something_else(arg)
    

    It’s almost the same thing, but it looks nicer, and you can have all your a‘s and b‘s and lists all sorted out before hand.

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

Sidebar

Related Questions

I'm not sure I am using ad-get-args and ad-get-arg right. For example, the following
The following does not compile: class Foo { public: Foo( boost::shared_ptr< Bar > arg
Arg. Inheriting projects is SO much fun. Especially when they don't work well, and
When I call perl's two-arg open() with a filename containing a trailing newline, the
Imagine the following situation: struct Args { bool arg; }; void thing(Args arg) {
I'm looking for something that works like Lisp's arg-supplied-p variables, to help differentiate a
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
println args println args.size() println args.each{arg-> println arg} println args.class if (args.contains(Hello)) println Found
Assume the following function prototype: void *function(int arg) . Is that a pointer to
I of course know that I can rename the init arg for an attribute

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.