Add up 2 identical values in a list

Hey everyone,

I have a list with element IDs of which some are duplicates. The IDs refer to walls and their surface areas, and if I have duplicates in the list, it means that one wall surface area is split up into 2, thus the individual values are incorrect because they need to be added together again.

So now I was thinking about filtering the duplicates in the ID list and turn that into a command to add up the 2 values in the referenced list of surface areas to get the actual complete wall surface area again. Preferably with Python.

Does anyone have an idea how I can manage that?

identity

You could try the following:

2 Likes

List.GroupByKey and Math.Sum would work.

1 Like

Came here to say this.

1 Like

Thanks for the clue, I never worked with List.GroupByKey… Can you help me out with the setup?

https://dictionary.dynamobim.com/#/Core/List/Action/GroupByKey

Hey again,

thanks for both answers, I tried the solutions and came across a problem:

1 List.GroupByKeyOnly
It works only when the list is flattened. But when flattened I loose the connection of the walls to the room, since one wall interacts with several rooms I am having a hard time reassigning the surface areas to the rooms.

2 Python Script
When running the script I get a warning:

Warning: IronPythonEvaluator.EvaluateIronPythonScript failed
Traceback (most recent call last):
File “< string >”, line 18, in < module >
IndexError: index out of range: 2

Also Dynamo within Revit has an issue using “areas” as a variable name in the codeblock because it’s a class name. Does it hurt when I change the name to something like “outareas” just in the codeblock node?

.
Do you know how to work that out?
Again, thank you for the help!

  1. try set lacing to longest. that should do the trick of not having the flatten it.

  2. can you show us what you typed in your python script and inputs for us to have a better understanding of what is the root cause of it?

  3. the naming doesnt matter as long as you know what you are doing.

1 Like

Depending on the list structure, list levels (@L2) may be more effective then lacing here. You’ll want that for both the groups and the keys.

List levels is key.

1 Like

Try this one. Basically, we create a new hash table with keys is each element in list1 and the value is each element in list2 accordingly. Then we run a loop, if it find the duplicated keys, then add up the value.

List levels works great! The python script works fine as well, but for some reason it changes the order of the values in my list.

Thanks a lot!