How to replace alternate value of Dictionary from the List

Hii all,

I have a dynamic Dictionary(of String,String)
Dict= [abc, 1,23,2,34,3,45…]

I have another dynamic List(String) as List= {“Jan 22”, “Feb 22”, “May 22”…}

I need to change the alternate value of Dictionary like if there’s “1” in Dict then it takes “Jan 22” or if there’s 3 in value of Dict then it should take “May 22” likewise.

So that My final Dictionary should be like this [abc , Jan 22,23,Feb 22,34,May 22,45…]

Please suggest me anything which will be helpful.

Thank You

@siddhi.rani

Can you show the dictionary properly please

which is the key and what is the corresponding value

{A:123,B:345}

here A and B are keys and 123 and 345 are values

cheers

Hi,

How about the following?

newDictionary = listStr.Zip(dict.Keys,Function(s1,s2) Tuple.Create(s1,s2)).ToDictionary(Function(t) t.item2,Function(t) t.Item1)

Sample20230429-6L.zip (2.5 KB)

Regards,

Here Key is ‘abc’ and the value is (1,23,2,34,3,45…) in the Dictionary.

Hi @Yoichi ,

The key of dictionary should not be change , only the alternate values should be changed like I explained above.
If the value of Dictionary contains ‘1’ then it must takes ‘Jan 22’ from the below List .
I hope you are now getting my doubt.

Thanks

HI,

Can you try the following?

newDictionary = dict.ToDictionary(Function(kv) kv.key, Function(kv) String.Join(",",kv.value.Split({","c}).Select(Function(s) if(dictFromList.ContainsKey(s),dictFromList(s),s))))

Sample20230429-6Lv2.zip (2.6 KB)

Regards,

1 Like

Hii @Yoichi ,
This is working in simple condition .

But in my case, I’ve assigned and giving data to Dictionary in one For Each activity and the ’ List ’ ( from which I need to replace the alternate value of Dictionary ) is created in another For Each activity.

So in this case it is not working.
Please tell me some different solution.

Thanks

@siddhi.rani

Please try like this

  1. Use a for loop on the dict.keys.ToArray() and change for type argument to string
  2. Inside loop use a assign and create variable of type array A1 and A1 = dict(currentitem).ToString.Trim.Split({","},StringSplitOptions.None).ToArray
  3. Inside the loop use one more for loop(currentitem2) with Enumerable.Range(0,A1.Count).ToArray and change type argument to integer
  4. Inside the second loop use if with (currentitem2 mod 2).Equals(0)
  5. On then side use assign A1(currentitem2) = L1(Cint(A1(currentitem2))) (L1 is list containing your dates)
  6. Outside the current loop and inside the fist loop use assign dict(currentitem) = String.Join(",",A1)

Hope this helps

Cheers

Hii @Anil_G ,

I’ve tried the solution given by you and i’m getting this error .
Please see the attached screenshot

Thanks.

@Anil_G
can you please share the code using the solution you mention.

@siddhi.rani

I missed a bracket please try now…and if you face issue show me the code you used

Cheers

Thanks @Yoichi .
It works.

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