Yes! Python 3.8 introduces the “Assignment operator” :=
, which allows you to define a variable within the local scope of a single expression (e.g. a comprehension). In your example, you would do this:
result = {(p := next(k for k in ('path', 'subdir') if k in e)): some_func(p)
for e in bad_structure}
Disclaimer: this will not work in any version of python before 3.8.