How to add values to an already existing key in dictionary

Hi!
I’m trying to add new values to an already existing key in my dictionary.
Let’s suppose I have the following dictionary:

my_dict {
‘key_1’: ‘value_1’,
‘key_2’: ‘value_2’
}

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:

my_dict_2 {
‘key_1’: [‘value_1’, ‘value_3’, ‘value_4’],
‘key_2’: ‘value_2’
}

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:

my_dict(“key_1”) = my_dict(“key_1”)+", "+ “value_3”+…

Which, in turn, returns this:

my_dict_3 {
‘key_1’: ‘value_1, value_3, value_4’,
‘key_2’: ‘value_2’
}
Unfortunately this doesn’t work for me.

Does anyone know a workaround?
Thank you!

1 Like

Hi @franrua02 ,

Correct only can u share your workflow or picture

Regards,
Arivu

1 Like

dictionary

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.

I hope this is clear enough!

@franrua02 you are going correct.
Can you share the dictionary datatype and the output/error you are getting?

Hi @franrua02,

  1. 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.
    dict {
   ‘key_1’: {‘value_1’},
   ‘key_2’: {‘value_2’ ‘value_3},
    }

where {‘value_1’} and {‘value_2’ ‘value_3} are type List.

  1. To do this you can first initialize three variables

  2. 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.

  3. Once you know all your strings are in the above list / lists, you are ready to add it to the dictionary variable using Add to collection activity

  4. Finally, if you want to check your dictionary has updated values you can use
    image

  5. Your output will look like this for index 0
    image

Here is the workflow you can refer: AddListToDictValue.xaml (12.6 KB)

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.

Hope this helps!

The same request was solved here: https://forum.uipath.com/t/filter-dictionary-values-dynamically-and-get-keys-of-matches

:slight_smile:

dict
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. :sob: :sob:

@franrua02,

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 :slight_smile:

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 :slight_smile:
Cheers!