Dictionary weird outputs

Hi all,

I have experienced since long time ago weird behavior when dealing with nested dictionaries such as this one:

When dealing with these sorts of dictionaries, I noticed that my Python code does not act as expected. You can see in the image that it does not show any outcome.

However, if I use C# methods…

Does anyone know why I sometimes it takes Python dict methods an sometimes C# dict methods?

Here I post the two different types that Dyname outputs

same code but a different way of parsing the JSON:

I’ve also noticed that nested dictionaries tend to drop display… I can usually get it back by restarting Dynamo and any host, but that’s problematic. Fortunately the data is there still so you can often (always?) get the data back by interrogating the keys if you recall them (if you don’t your code might be too complex to scale well). The issue has always cleared after restarting the host application (or Dynamo itself if you’re in Sandbox).

1 Like

Hey @jacob.small , thanks for the quick response.

I also noticed that if you either restart Dynamo or disconnect prior wires, run, reconnect them again, and rerun the info shows up again. However, when you want to outcome new info from the Python node, let´s say you wrote some new lines, the output will be blank again. This does not mean the info does not exist.

Despite knowing this workaround, it might be a pain in the neck to carry out this action every single time… In the end, when you are dealing with very long and complex Python codes, where debugging at almost every single line of code you type is needed, it is very time-consuming.

My main concern and the reason I ended up opening this topic are:

  1. Is this something that only occurs in some specific Dynamo versions? Currently, I am running Dynamo in Revit 2023 (cannot tell you a specific Dynamo version at the moment) but if this is something that is sorted out in newer versions, it will be a huge problem because it means my codes will not work in new Revit/Dynamo versions.
  2. I still cannot get why when typing lines of code in the Python node, we need to figure out if the method to apply is coming from either C# (System.Collection.Generic.Dictionary´2: .Values(), .Keys()…) instead of Python methods (DesignScript.Builtin.Dictionary: .items(), .values(), .keys()…). I have some Python nodes with a mix of both.

Cheers.

Not trying to criticize here, but give some assistance/support for the growth of community members in general. The pain you are feeling here isn’t just a sign that Dynamo has a bug, but also your authoring process has a bug. Line by line text output means you’re working very inefficiently with any editor/compiler. I recommend you build a logger, or providing a ‘progressive’ output.

Now that this is out of the way,

Nope. As far as I know it has been this way from 2.3 when Dictionaries were introduced. The good news is that if they start showing up without failure you’ll be good to go.

Dynamo is C# based. As is Revit, Civil 3D, and every other part of the ecosystem… so in most respects Python is the outlier - using the C# dictionary by default might be more successful, however even raw C# development with complex enough multi-level dictionaries will result in the missing display issue.

Hi @jacob.small , thank you again for the response. Much appreciated.

Maybe I did not express my words clearly. Sorry about that.

With debugging at each line of code I meant that I need to output the type of data to identify whether the type is a System.Collection.Generic.Dictionary'2 or DesignScript.Builtin.Dictionary. Once I can spot the type, I can apply either C# methods or Python methods.

Cheers.

Before you output the data is not a Dynamo dictionary… well unless you’re doing something VERY odd. You can output the class of the collection to check it and can even review over time. Something like this:

dbg = []
dictionary = {}
dbg.append(dictionary.__class__)
dictionary[“Key1”] = 1
dbg.append(dictionary.__class__)
dictionary[“Key2”] =2
dbg.append(dictionary.__class__)
OUT = dbg

Hi JM,
Some python tips to work with Dictionaries

# Check if key is in dict
if my_key in my_dict:
    # do stuff

# Access key - if it exists
my_values = my_dict.get(my_key)  # Returns None if key does not exist
if my_values:
    # do stuff

Running a for loop over a variable overwrites the variable
Also you are trying to get the values like they were a property, use the values() method

# list (python 2) or generator (python 3) of variables
my_values = my_dict.values()

# Your usecase
data = rpt_data.get("Rpt_Bo_OutterRing")
if data:
    OUT = list(data.values())

If you’re having issues with type conversion I would convert to JSON string and stay in python -or- stay in Dynamo. This issue is most likely due to shallow type conversion which only checks the first container, the other alternative is writing a ‘deep type converter’

1 Like