Getting the first non None value from list

后端 未结 5 1638
盖世英雄少女心
盖世英雄少女心 2021-01-31 13:25

Given a list, is there a way to get the first non-None value? And, if so, what would be the pythonic way to do so?

For example, I have:

  • a = objA.addr
5条回答
  •  庸人自扰
    2021-01-31 14:09

    Adapt from the following (you could one-liner it if you wanted):

    values = (a, b, c, d)
    not_None = (el for el in values if el is not None)
    value = next(not_None, None)
    

    This takes the first non None value, or returns None instead.

提交回复
热议问题