- Home /
get value of Dictionary keys via index number?
I am wondering if I can get or extract the string name of the Key in a Dictionary
I have seen examples accessing the key from inside a foreach, but I need a single index approach. The index will be decided by random int.
public void rng_head()
{
int rnd_q = Random.Range (1, 5);
int rnd_t = Random.Range (1, 5);
// THIS IS HOW I WOULD GET FROM ARRAY or 2D ARRAY
//string output = head_quality [rnd_q].ToString () + " " + head_type [rnd_t].ToString ();
//string output_id = head_quality [rnd_q].ToString () + " " + head_type [rnd_t].ToString ();
// BUT I WANT TO GET FROM DICTIONARY??
string output = dict_head_quality [rnd_q];
string output_id = dict_head_type [rnd_t];
// dictionary should keep perfectly accurate display name vs id_name
// so when items are generated they have a nice display name, but also
// an exact, lowercase, id name
}
Answer by fafase · Jun 14, 2015 at 07:50 PM
A dictionary does not use index since it is not its initial purpose. A dictionary uses key-value relation. It does not so because items are not stored continuously in memory. You have one reference which knows where all items are. But none is first or last. Like when you look in the fridge, you know the eggs are on the door top shelves and the vegetables are on the bottom drawer but which is first then?
You could build one with int,object relation where the int is the key and works as an index. It would be like a list with non continuous indexes.
Your case looks like you should think of list/array instead.
If you really need to store in a dictionary and want to have something random out of it, maybe something like this:
string GetRandomValue(Dictionary<string,string> dict) // Dunno what is your dictionary types...
{
int rnd = Random.Range (1, 5);
int index = 0;
foreach(var kvp in dict){
index++;
if(index == rnd){
return kvp.Key; // According to question, you are after the key
}
}
return null;
}
Answer by Erfo77 · Aug 08, 2021 at 06:46 AM
You can also index the dictionary values by yourself enveloping the key-value pair you are interested into a KeyValuePair and associating it with an int like so:
Dictionary<int, KeyValuePair<string, int>> indexedDictionary = new Dictionary<int, KeyValuePair<string, int>>
{
{0, new KeyValuePair<string, int>("my entry", 13) },
{1, new KeyValuePair<string, int>("whatever", 5) },
{............}
};
In this case it's pointless to use a Dictionary. A list of KeyValuePairs would make much more sense. The point of a dictionary is to provide fast lookup of keys that are not indices. packing KeyValuePairs like this into a List or dictionary means you loose the ability to lookup keys fast.
If you need both, a dictionary lookup of keys and you want to randomly select keys through an index, you can create a seperate string list / array that contains all keys. So you can pick a random key from that array / list and use that key in the dictionary.
Indexing a dictionary is not what a dictionary was intended for. That's for sure. That said, if you do need to access its values and keys also via an index reference, depending on the use case a List may not be appropriate as in a List the indexes will change as you remove or add to it