Setting dictionary values to variables?


#1

I’m trying to learn how to make custom dictionaries work, as they seem to really help simplify workflows when done correctly. I have a sample shortcut here that attempts to take a set of text, split the text, assign each text segment to become a dictionary value, and then use that dictionary to create a table. I’m clearly doing something wrong, but as I’m still learning about dictionaries, I’m not sure what. Is it even possible to create a dictionary in this way? Any ideas?


#2

To build one from text, the best way is probably to express it as JSON and then use the get dictionary from input action. After that, you can enumerate the keys and iterate over them in a repeat loop.

But unless you want to pick out specific values by name, the above could in effect be achieved just by using a list and the split approach you began with.

Hope that helps.


#3

You don’t need to pre-build the dictionary items. If a key does not exists in the dictionary, Set Dictionary Value will create it for you.


#4

True, you don’t have to pre-build the items, but I’m struggling to see the practical benefit of doing this. :frowning_face:

To me it seems like there is less overhead for creation, and at worst, an equivalent in access, to simply splitting into a list and then referencing the item by index. i.e. I see no imperative to coerce the data into a dictionary over an originating list, if it is an index based list to begin with.

It’s quite possible I’m overlooking a key scenario. Is there one, … or more where this would be a key approach?

  • The only thing I can think of so far is if this was specifically a data transform for inclusion of a sub-set of data in a larger dictionary and it was already available as a separated text string. But that’s quite specific.

  • After a quick test, just in case, I don’t think there’s a performance benefit in Shortcuts using a dictionary vs. an index references either.


#5

I don’t think there is a benifit for performance but you can cut down on work and add privacy which are my two reasons for a dictionary over a list referenced via index (and a third for whatever I find simpler).

If you use dictionaries you can share a shortcut and then strip out sensitive data while keeping the dictionary variables in place then have the super fill in the data with import questions.

Also if your going to use common variables across multiple shortcuts (ie a shortcut to say access redditt that you could use the run in shortcut option to treat them as dependencies.

Or it can just “feel” neater to reference one big dictionary throughout a shortcut rather than getting and references various lines of a larger list.

In the end I think it’s prefrence. More and more I am using dictionaries but there are plenty of times a mess of variables or a indexed list works just as quickly


#6

This kind of problem definitely lends itself to a list. You can take the lines of text and run them through a single “Join Text” action with a custom | delimiter, then bracket that in | for your table output. See this example


#7

I agree with your points above, though they appear to be regarding other use cases. I don’t believe they apply to the sort of example above, for converting text into a dictionary with indexed identifiers which is what my query specifically pertained to. :man_shrugging: