Python idiom to return first item or None

后端 未结 24 1983
清酒与你
清酒与你 2020-12-07 07:46

I\'m sure there\'s a simpler way of doing this that\'s just not occurring to me.

I\'m calling a bunch of methods that return a list. The list may be empty. If the

24条回答
  •  隐瞒了意图╮
    2020-12-07 08:20

    (get_list() or [None])[0]
    

    That should work.

    BTW I didn't use the variable list, because that overwrites the builtin list() function.

    Edit: I had a slightly simpler, but wrong version here earlier.

提交回复
热议问题