Sorry if the title is a bit unclear, I didn't know how to really summarize the issue.
If I some list of objects like this:
class Item:
x = 0
y = OptionalMember() # this can be None
class OptionalMember:
z = 1
And I want to use a list comprehension to collect from a list of Item
objects, but including the value z
when y
is present, otherwise just taking None
. If y
was always present, of course it would simply be
xs = [(item.x, item.y.z) for item in items]
but then
AttributeError: 'NoneType' object has no attribute 'z'
so how to do it?