My goal, in this example, is to add the values on this list: [‘value_3’, ‘value_4’] either to key_1 or to key_2.
More explicitly, I intend to get a new dictionary as showed below:
I’ve read about some approaches that suggest to replace ‘value_1’ with a new string comprised of the old value and all of the new ones with an assign activity:
Name of dictionary: out_DiccArchivosDefectuosos
1 activity: I check if key ‘Archivo’ already exists in dictionary.
2 activity:
If it does (Then):
I proceed as showed above: Replace existing value with new string.
If it does not (Else):
I create it and add first value.
The activities above are inside of a For Each activity: I’m iterating over some files.
In that case I suggest you define all your values as a List. This is because List type will let you add items to it independent of which key you want to update.
Then you can in a for loop add any number of values to the list. In this example, I have used two hardcoded lists, you can generalize the approach in a for loop such that each time you run the loop a new list is initialized and the string items are populated to it accordingly.
Improvements: If you can find an elegant way to directly add Keys(String), Values(List(ofString)) to the DictExample, you could use Add To Collection activity too.
In my example, I have used the key to assign values in Step 4.
I’m not getting any error. I just want my dict to have the structure shown in my_dict _2 (see above) instead of that of my_dict _3, which is what I’m getting.
Hey, Jeevith. Wow, thorough answer. This could work, I’ll try it! Thank you!
Although I was kinda hoping to get it done with a one line code like in Python with the append method.
I .append() to your thought process, as I come from the python world as well!
When I started with UiPath I felt the same: Why do we have to be so aware of initializing variables? Why make things so complicated?
But trust me when I tell you this. .Net grows on you quite quickly, not because the methods are easy or the documentation from Microsoft is a boon, but because these methods you learn via UiPath can easily be extended to either C# or PowerShell. Most often you will end up needing one of these in a large automation project. The dots will then connect
Do try the solution out, and if you have any questions or improvements do share them in this threat. I would like to learn from them as well
Cheers!