Iterative Loop based on user selection - I.e. count of selected objects then run code

I am working on filtering, combining and counting elements (from lists) into respective groups to spit out grouped elements as a list of strings. The problem i can’t seem to figure out (and i think the answer may be a scrips but any help is much appreciated) is that this program is looking through element parameters based on elements selected by a user.

this means that the number of elements will change every time, and further downstream if the elements have differing parameter values it will change the number of elements on each list.

So I have a Dynamo script that is working to get me the result that i want, but the only way i can figure out how to get all of the results is to create an upper limit of the maximum possibilities…the final result being hundreds of nodes, and a pretty ugly, heavy graph.

I need help finding a node or writing a script that will loop over my initial selection and run the next step based on the number of key values generated. (i.e. if i have 2 parameters i only want 2 instances of the downstream part of the script to run) Right now because there are 10 possible parameter values, i am using a get item at index with a hard coded double 1-10, which means my script end up with a very large tree very fast, as well as very commonly having quite a few nodes with no input.

thank you for your help!

My first thought is to use a dictionary, but if you can post your DYN it would be very helpful.

1 Like

It’s a bit difficult to help you if don’t see anything - please read this How to get help on the Dynamo forums
What @cgartland wrote seems sensible to me - having a dictionary ‘channel’ your different options. If you need help on how to do this lets see where you got so far.

Appreciate the response, right now my script is pretty massive (the attached screenshot is about 1/10th of it), ill try to get a boiled down version online at some point soon.
Essentially though i am selecting elements, and sorting/grouping by size; then i would like the elements to remain in their separate size groups, and for each of those groups to be sorted by another parameter (say height). and then after the sublists have been grouped, i would like to run the same operation again against a third parameter (say manufacturer). the end result being a list with sublists that are each unique groups of instances that meet all 3 criteria i.e.:

(3) 50" TV, 34" high, samsung
(2) 45" TV, 32" high, samsung
(3) 50" TV, 34" high, LG

the issue i am having isn’t with doing any of the operations, It is that the elements being looked at are selected in revit by the user, who will not have access to the script, so if they select 5 items or 25 items each number of parameters changes.
right now i am using a “get item at index” and a hard coded index number. If I could make this flexible though and only run the operation based on how many indices exist based on the selection.

@cgartland, I thought of a dictionary too, as well as “sort list of lists” the problem is then combining the values and keys correctly, I have tried this and ended up with 3 sorted lists, but chopping the lists correctly and combining them and counting the items per group is something i cant seem to solve.

I have seen it done in python, but I haven’t had any sucess at writing a script within dynamo that can do what i want (pretty new to python)

i’ll try to post something today if i can thanks for looking

I don’t think this is exactly what you’re trying to achieve, but is this somewhat similar?:

elements = IN[0]
products = zip(IN[1], IN[2], IN[3])
"""
Assign elements to properties, e.g.
{
	('50', '10', 'Mfgr. A'): 1,
	('45', '10', 'Mfgr. B'): 2
}
"""
products_dict = dict(zip(products, elements))
# Hard coded valid product list as an example here.
# Ideally this would be another input from Dynamo 
# which would then be cast to a list of tuples.
products_valid = [('50', '10', 'Mfgr. A'),
				  ('40', '20', 'Mfgr. C')]

elements_out = []
products_out = []
for product, element in products_dict.items():
	if product in products_valid:
		elements_out.append(element)
		products_out.append(product)
OUT = elements_out, products_out

Yes!
thank you @cgartland. I really appreciate your work on this. I am a little confused how it is working though, is the 0 index in the python output a count?
based on the list and your inputs none of the sublists contain all of the same parameters so i would assume the count would just be (1) because each item is unique, they should only be grouped if all the parameters are the same…am i reading that wrong?
I do have inputs i can plug into the script to get data from my project so i’m going to play with this a little later (once i get all of the RFIs of my desk agh!). I’ll post and let you know how it goes.

thank you again for your help I really appreciate your work on this script^

No problem. Just realized a slight problem with my script–I was providing duplicate keys in my dictionary, so the corresponding elements were being lost. Here is a modified version which works properly:

elements = IN[0]
products = zip(IN[1], IN[2], IN[3])
"""
Assign elements to properties, e.g.
{
	('50', '10', 'Mfgr. A'): 1,
	('45', '10', 'Mfgr. B'): 2
}
"""
products_dict = {}
for product, element in zip(products, elements):
	if product not in products_dict:
		products_dict[product] = [element]
	else:
		products_dict[product].append(element)
# Hard coded valid product list as an example here.
# Ideally this would be another input from Dynamo 
# which would then be cast to a list of tuples.
products_valid = [('50', '10', 'Mfgr. A'),
				  ('45', '10', 'Mfgr. B')]

elements_out = []
products_out = []
for product, element in products_dict.items():
	if product in products_valid:
		elements_out.append(element)
		products_out.append(product)
OUT = elements_out, products_out

Filter Products.dyn (20.8 KB)