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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:55:37+00:00 2026-05-28T03:55:37+00:00

for the following code a =func() if a != None: b.append(a) a can be

  • 0

for the following code

a =func()
if a != None:
    b.append(a)

a can be assigned to None, is there a way to avoid the if statement and only use one line of code?

original problem is the following

import xml.etree.ElementTree as etree

r = etree.parse(f).getroot()
b = etree.Element('register',{})

a = r.find('tag_name') # a may get None if did not find it
if a != None:
    b.append(a)

ok, I used all the answers and got this, personally I think it’s the most complex python I have ever wrote so far, lol

NS_MAP = {
    'spirit' : 'http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4',
    'app' : 'http://www.app.com/SPIRIT-app'
    }

mp=etree.Element('MemoryProperty', {'version':'alpha'})
mpt=etree.ElementTree(mp)


def copy_tags(tp, op, p, tn, ns='spirit'):
    c =  p.find('{%s}%s'%(NS_MAP[ns],tn))
    if c is not None:
        (op == '<-') and tp.append(c)
        return c    

for reg in regs:
    te = etree.Element('register',{})
    copy_tags(te,'<-',reg,'name')
    copy_tags(te,'<-',reg,'addressOffset')
    copy_tags(te,'<-',reg,'access')
    (lambda e, t: copy_tags(te,'<-',t,'usageConstraints',ns='app') if t is not None else None)(te, copy_tags(te,'|',reg,'vendorExtensions'))

    mp.append(te)

mpt.write('map_gen.xml')
  • 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-28T03:55:38+00:00Added an answer on May 28, 2026 at 3:55 am

    If you can call func() beforehand, and you want to combine the test and assignment statements into a single statement, then you can do this, with an if-else expression:

    b += [a] if a is not None else []
    

    If a is not None, then this will add [a] to b — essentially the same operation as b.append(a)

    If a is None, then this will add [] to b, which will leave b unchanged.

    This won’t work unless b is a list, or at least supports “+=” in-place addition. If it doesn’t — perhaps it’s some custom object, then you should be able to do this:

    (b.append(a) if a is not None else None)
    

    This is an expression, evaluated for its side effects, and then thrown away. If a is None, then the b.append(a) call will never be executed. In either case, the value of the expression is None, but we don’t care about it, so it gets ignored.

    Now, if you want to combine the func() call with this, then you’ll have to do something different in order to avoid calling func twice. If you can use the “+=” syntax, then you can do it like this:

    b += filter(None, [func()])
    

    filter(None, <list>) returns the list with all false elements (None included, but also 0 and []) removed. This statement, then, will add either [func()] or [] to b.

    [Edited]

    Finally, for the worst case scenario: If you can’t call func() more than once, and you can’t use b += <list>, and you need to accept 0, “”, [], etc, and only exclude None, and you need it all on one line, here’s the ugliest line of code yet:

    (lambda l, a: l.append(a) if a is not None else None)(b, func())
    

    This is essentially @ekhumoro’s solution, compressed into one line. It defines an anonymous function, calls it, discards the value, and then discards the function, all for the sake of the side effect.

    Now, this is a single line, but it’s certainly not easier to read or understand than the original code. If I were you, I’d stick with the original, or go with @ekhumoro’s idea of just defining a helper function and using that.

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

Sidebar

Related Questions

How can i make the following code compile? Action<MyClass<object, object>> func = x =>
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
All, Consider the following code: void func(void) { int a; printf (%d, a); }
I need a device version of the following host code: double (**func)(double x); double
I have the following representative code: __global__ void func() { register ushort4 result =
I have the following code Expression<Func<IPersistentAttributeInfo, bool>> expression = info => info.Owner== null; and
What will be solution in the following code Class A{} void func(){} printf(%d,%d,sizeof(A),sizeof(func));
I have a the following code: #include <iostream> using namespace std; void func(char *
Take the following code: $target = 'NAME FUNC LPAREN P COMMA P COMMA P
I have the following code: public OTestTable GetTestCode(Func<TestTable, bool> whereClause) { return CoreContext.TestTables.Where(whereClause).Select(TestTableMap.DataToObject).FirstOrDefault(); }

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.