# How to infere data with Python based node

**URL:** <https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746>\
**Category:** Developers\
**Tags:** python, dynamo\
**Created:** [December 28, 2020, 2:28pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746 "2020-12-28T14:28:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fyffe](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@fyffe](https://forum.dynamobim.com/u/fyffe)\
**Post date:** [December 28, 2020, 2:28pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746/1 "2020-12-28T14:28:22Z")

</div>

Hi all!  
I would ask you a support to identify errors in a IronPython based node for if-elif-else statement.

I have breakdown of Dynamo when running the pipeline I tried to develop.

This Python node should have as Input _n_ lists of _n_ items that are parameters and values inserted, and get, in the BIM model. It works when I use one input, but not the lists. I admit I am not great enough to write a Python script, so I think it is a very stupid error I do (even if the visual studion Validation does not give me any alert.

The code I write is:

```
s=IN[0]
l=IN[1]
n=IN[2]
g=IN[3]
r=IN[4]
t=IN[5]
f=IN[6]

if (s == "≤1/8" and l == "assenti" and n == "≤1/5 ≤50 mm" and g == "≤2/5" and r == "≤1/14 (7%)" and t == "≤1/10 (10%)" and f == "ammissibili, purchè non passanti"):
	c="1"
elif (s == "≤1/5" and l == "assenti" and n == "≤1/3 ≤70 mm" and g == "≤2/3" and r == "≤1/8 (12%)" and t == "≤1/5 (20%)" and f == "ammissibili, purchè non passanti"):
	c="2"
else:
	c="3"
# Assegnare l'output dell'utente alla variabile OUT.
OUT = c

```

Here, the input are 6, but can be more, according to the number of elements I go to select in the model (it can vary case by case), so this is another question to be inserted as input definition. The output should be a list itself in a number equal to the number of elements selected in the BIM model.

Can you help me?

I will be very glad

Bests

Silvana

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [December 28, 2020, 3:27pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746/2 "2020-12-28T15:27:39Z")

</div>

> [@fyffe](#):
>
> It works when I use one input, but not the lists.

This is because you aren’t iterating over the list of inputs, but instead working with each list as a number. You need to wrap the entire statement in a for loop, or utilize list comprehension to do so.

However because you indicated you have an N dimensional list, that it ‘it could be a list of values, or a list of lists of values, or a list of lists of lists of values’, you may find it easier to wrap the python on a custom node to facilitate the unknown structure of data and enable list at level and lacing per standard Dynamo.

---

<div class="post-metadata">

**Author:** ![fyffe](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@fyffe](https://forum.dynamobim.com/u/fyffe)\
**Post date:** [December 28, 2020, 4:16pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746/3 "2020-12-28T16:16:24Z")

</div>

Thank you @jacob.small,  
I am going to try to use the custom node (a new diyf file), instead of using a Python Script node.

I was studying the for loop (but with no real awareness about the pros), but I do not how to utilize the list comprehension.

I am going to deep these concepts and try!

Thank you very much!

---

<div class="post-metadata">

**Author:** ![fyffe](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@fyffe](https://forum.dynamobim.com/u/fyffe)\
**Post date:** [December 28, 2020, 4:29pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746/4 "2020-12-28T16:29:20Z")

</div>

```
s=IN[0]
l=IN[1]
n=IN[2]
g=IN[3]
r=IN[4]
t=IN[5]
f=IN[6]

c = []

def method_name():
    for item in lst(s,l,n,g,r,t,f):
     if (s == "≤1/8" and l == "assenti" and n == "≤1/5 ≤50 mm" and g == "≤2/5" and r == "≤1/14 (7%)" and t == "≤1/10 (10%)" and f == "ammissibili, purchè non passanti"):
        c="1"
     elif (s == "≤1/5" and l == "assenti" and n == "≤1/3 ≤70 mm" and g == "≤2/3" and r == "≤1/8 (12%)" and t == "≤1/5 (20%)" and f == "ammissibili, purchè non passanti"):
        c="2"
     else:
        	c="3"
     return c, item

def method_name():
    c, item = method_name()
    return c, item

c, item = method_name()

```

I have the same breakdown (dynamo and Revit close)

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [December 28, 2020, 6:47pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746/5 "2020-12-28T18:47:30Z")

</div>

Are your variable lists of a matching structure and depth? Ie: do you have 3 items in each list? Or could s have 5 items and n have 2?

---

<div class="post-metadata">

**Author:** ![fyffe](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@fyffe](https://forum.dynamobim.com/u/fyffe)\
**Post date:** [December 29, 2020, 3:11pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746/6 "2020-12-29T15:11:15Z")

</div>

each list has the same number of items (equal to the number of selected elements)

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [December 29, 2020, 7:02pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746/7 "2020-12-29T19:02:43Z")

</div>

Hello @fyffe  
a trick using a dictionary and list comparison to avoid multiple **and** operator

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/2/52a813cef62cbc3984680a88b3d3fd566247456c.jpeg)

```
import sys
def getKeybyValue(mydict, searchlst, default):
	for key, valuelst in mydict.items():
		if list(valuelst) == list(searchlst):
			return key
	return default		

dataEnteringNode = IN
#transpose if list contain sublist
if all([isinstance(i, list) for i in dataEnteringNode]):
	dataEnteringNode = [i for i in zip(*dataEnteringNode)]
else:
	dataEnteringNode = [dataEnteringNode]

mydict = {
"1": ["≤1/8","assenti", "≤1/5 ≤50 mm", "≤2/5", "≤1/14 (7%)", "≤1/10 (10%)" , "ammissibili, purchè non passanti"] ,
"2": ["≤1/5", "assenti", "≤1/3 ≤70 mm", "≤2/3", "≤1/8 (12%)", "≤1/5 (20%)", "ammissibili, purchè non passanti"] 
}
out = []
for sublist in dataEnteringNode:
	i = getKeybyValue(mydict, sublist, default = "3")
	out.append(i)
	
OUT = out

```

_Note: when you post some code please use_ ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/9/a9041e7168476953a9b3ad6385455e2d4f2844e2.png)_button after selection of your code_

---

<div class="post-metadata">

**Author:** ![Yien](https://avatars.discourse-cdn.com/v4/letter/y/ecd19e/32.png) [@Yien](https://forum.dynamobim.com/u/Yien)\
**Post date:** [December 29, 2020, 7:05pm UTC](https://forum.dynamobim.com/t/how-to-infere-data-with-python-based-node/58746/8 "2020-12-29T19:05:58Z")

</div>

i would use a loop and a zip method here. but to catch missing or uneven elements from list you can use zip\_longest() from itertools.
