Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the key/value of a dictionary

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.

like image 972
bbesase Avatar asked Jul 23 '13 17:07

bbesase


People also ask

Can we access the keys from values in dictionary Python?

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.

How will you get all the keys and values from the dictionary?

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.


2 Answers

.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
like image 134
Curt Avatar answered Oct 03 '22 00:10

Curt


Try like this,Use item to get corresponding value

letter = " ";

If keystrokeDictionary.ContainsKey(letter) Then
letter = keystrokeDictionary.Item(letter)
End If

Return letter
like image 43
Sivashankar Avatar answered Oct 03 '22 01:10

Sivashankar