How do I check if a Variable exists in a Dictionary (Key, Value)?

Example Dictionary =
{Sweden, Nordics}
{Finland, Nordics}
{Denmark, Nordics}

Variable = Sweden

if Variable in Dictionary, Print Value

Expected Output = Nordics

Unfortunately, the “Sequence” feature on UiPath does not include Microsoft’s “Key Exist” action. “Key Exist” is only available for Flowcharts.

Can’t you use contains ?
Seems that it has a containsKey method, maybe it can work ?

Yes, I used this: IF Dictionary.ContainsKey(Country), Message Box Prints Dictionary.Value(Country)

What if I want to take it to the next step and compare the Value to another variable?

IF Dictionary.Value(Country) = Nordics then proceed with next steps…

Maybe i don’t understand but if you want to iterate you can do a For each activity and then for each item in your dictionnary you can put your conditions and your actions.
But if you do that you can just do item.key or item.value to know the key or the value

You can do YourDictionary(Key) to obtain the value. Maybe it was your question, sorry for the precedent answer if it don’t help you at all ^^’

To clarify, I’d like to do two checks.

Check 1

  • I enter a Variable
  • Does this Variable match the Country (Key) and Region (Value) outlined in the Dictionary?
  • If Yes, proceed to Check 2
  • If No, terminate

Check 2

  • Does the Region (Value) = Nordics?
  • If Yes, continue
  • If No, terminate

Don’t think I need to iterate in a for loop here?

if YourDictionary.contains(“Sweden”) then
if YourDictionary(“Sweden”).equals(“Nordics”) then
– Continue
– Terminate
– Terminate

Nope, you basically wrote down your own solution there. Just a regular sequence.
Keys are unique in a dictionary.

1 Like

BINGO! This was the one I was looking for. Thank you so much @Constant_Roux. Bless you.

1 Like

Surround it with try-catch. Otherwise if ‘sweden’ doesn’t exists in your dictionary it will throw an exception.

Hi Rene, thanks for the recommendation. Do you mean I should use trycatch instead of an IF statement in the Sequence Activity?

The if statement should be surrounded by an try-catch. (Right click on the if-activity and lookup for ‘surround with try catch’)

So if your dictionary contains ‘sweden’ and the region is equal to ‘nordics’ you want to do something and if the region is not equal to ‘nordics’ you want to do something else.

But when there is no entry equal to ‘sweden’ in your dictionary you want to catch the exception and follow up with another activity e.g. send an email to someone…

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.