Access items in a list

The output of my python function seems to be a list. If I try to access it using the usual . notation inside a select riskscape gives me an error

- Property consequence of {..., consequence=>List[{NIWA_Damage_Ratio=>Floating}],...} is not a struct - was List[{NIWA_Damage_Ratio=>Floating}]

How can I fix this?

Hi John,

There’s a couple of different approaches, depending on what exactly you want to do.

Currently you have a list of structs, which makes things a little complicated. If you just wanted the mean/max/percentile value from the list, then you could change the consequence into a list of floats, and then use list aggregation. This would look something like:

mean(map(consequence, x -> x.NIWA_Damage_Ratio)) as mean_DR

You could probably then simplify things further by changing the return type of your Python function to be use float instead of a struct. Then the pipeline code would simply be:

mean(consequence) as mean_DR

If you wanted to pull a specific index out of the list, then it’s a little convoluted, but you could do something like this to pull out the first item in the list:

list_to_columns(consequence, { number: 5, prefix: 'item' }).item1.NIWA_Damage_Ratio

Here, the number is the max length that you expect the list to be.

The other option is to use the unnest() step to unnest the list, and then you can process each item in the list individually. Be aware that this will duplicate everything else in that row of data though, e.g. if the list is 10 items long, then you will end up with 10 copies of the input exposure after you unnest the list. For example, if you’re using all-intersections sampling and processing each item in the list then you may want to scale the losses for each item in the list.

Hope that helps,
Tim

Thanks Tim there is four items in the list that python is returning and this code runs

        round(mean(map(consequence, x -> x.NIWA_Damage_Ratio))) as NDR,
        round(mean(map(consequence, x -> x.NIWA_Repair_Cost))) as NRC,
        round(mean(map(consequence, x -> x.Damage_Ratio))) as DR,
        round(mean(map(consequence, x -> x.Repair_Cost))) as RC,

but I don’t love it. I will look into adjusting the python code