Creating dict of dicts in Max

Jason Aylward's icon

I'm trying to create a dict of dicts where the key is a float and the value is another dict. I think I have all the data ready but I have not managed to get it working in pure Max.

Any ideas or examples of how that works?

I've uploaded what I'm working on with comments. My goal is to iterate through each note in a clip and generate a dict of {<start_time>: {"start_time": <start_time>, "duration": <duration>, "pitch": <pitch>"}}

clip_dict.maxpat
Max Patch

TFL's icon

Why do you necessarily want a dict with start time as key? What if two notes have the same start time? Would you have an array of dicts as a value?

It's probably easier to create an index of note ids with their start time and use that id to retrieve your note infos from the original dict (or a rebuilt version of it with the note_id as keys).

Jason Aylward's icon

In my case, I really only care about start_time. Since sorting a list of dicts by a specific key seems to be absurdly complicated, I'm maintaining a sorted list of start_times and then using that to pull the dict of note information from the base dict of start_time:note_info.

Regardless of use case, I'd love to see an example of building a dict of dicts, especially when the keys are not known ahead of time. At the very least, I'd love to know that it's impossible and not just underdocumented because I feel like I'm wasting time trying to get a square peg through a round hole.

TFL's icon

To create a dict of dicts, you can simply send a dictionary message into a [dict.pack] or send a "set name dictionary my_dict" message to a [dict], where 'name' is the key you want to set and 'dictionary my_dict' the dictionary to use as a value.

Max Patch
Copy patch and select New From Clipboard in Max.

JSON dictionaries aren't meant to be sorted, that's why there is no 'sort' method for [dict].

Here's an example building a dict with all notes stored with the start_time value as a key. If multiple notes have the same start_time, that start_time key will have an array of dicts instead of a single note dict as value. See the key "2.5000" to see an example of multiple notes with the same start time.

Max Patch
Copy patch and select New From Clipboard in Max.

Jason Aylward's icon

Wow, thank you for taking the time to build out this example. Not only does this solve my issue but there's a few other elements in your examples that I haven't seen before. Notably tosymbol and buddy.

I swear I spent hours trying so many combinations to get a nested dictionary to work. Your first example is so simple, I don't know how I didn't try it. Thanks again.