How do I access the key/value of a dictionary? I have this code:
Try
If keystrokeDictionary.ContainsKey(letter) Then
keystrokeDictionary.Keys.Equals(letter)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return letter
Letter
is a single letter that gets passed in with just its string value. Then keystrokedictionary
is a dictionary that the information looks like this:
{[65, 0]}
{[66, 1]}
{[67, 2]}
Where the key is the first number and the value is the second number. Since my code isn't looking at the key/value right, it's not returning a letter, and therefore not working.
We can also fetch the key from a value by matching all the values using the dict. item() and then print the corresponding key to the given value.
Using dictionary. The built-in Python method items() is used to retrieve all the keys and corresponding values. We can print the dictionary's keys and values by combining the items() method with a for loop. This method is more practical if you wish to print keys one at a time.
.Equals()
is not an assignment operator, but a comparison function.
Try
letter = " ";
If keystrokeDictionary.ContainsKey(letter) Then
letter = keystrokeDictionary.Values(letter)
End If
Return letter
Try like this,Use item to get corresponding value
letter = " ";
If keystrokeDictionary.ContainsKey(letter) Then
letter = keystrokeDictionary.Item(letter)
End If
Return letter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With