python - Is there any way to return value in list comprehension? -
i wonder if can return value in list comprehension like
[return role_ role_ in self.roles if role.name==parameter] of course doesn't work. @ moment had use classic loop
for role_ in self.roles: if role_.name == parameter: return role_
use next() function generator expression:
return next((role_ role_ in self.roles if role_.name == parameter), none) this return first matching role_ value, or none if there no such value.
you can't use list comprehension, no, because not producing list.
Comments
Post a Comment