The code above looks simple enough that it is efficient as is. If you are looking to simplify your code further you can use this example below:
Expression:
hierarchy_creator_feet(!highway!)
Code Block:
def hierarchy_creator_feet(p_highway):
hierarchy = 0
if p_highway in ('footway', 'pedestrian', 'residential', 'living_street', 'crossing', 'track', 'path'): hierarchy = 3
elif p_highway in ('tertiary', 'tertiary_link', 'unclassified', 'steps', 'service'): hierarchy = 23
elif p_highway in ('secondary', 'secondary_link', 'primary', 'road'): hierarchy = 23
elif '[' in p_highway: hierarchy = 33
return hierarchy
or
def hierarchy_creator_feet(p_highway):
hierarchies = {
3 : ('footway', 'pedestrian', 'residential', 'living_street', 'crossing', 'track', 'path'),
23 : ('tertiary', 'tertiary_link', 'unclassified', 'steps', 'service'),
33 : ('secondary', 'secondary_link', 'primary', 'road')
32 : '['
}
hierarchy = 0
for a , b in hierarchies.items():
if p_highway in b or b in p_highway: hierarchy = a
return hierarchy
If you are able to reduce your code to the simplest level for both the result and ease of troubleshooting then it is already efficient enough. There are ways to write simpler code, but it mostly boils down to preference at that point.