Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding key/value pairs to a dictionary

Tags:

c#

i am using a dictionary to store some key value pairs and had a question on the best way to populate the dictionary. I need to do some other operations in order to find and add my key value pairs to my dictionary. After those operations i may have found a key/value to add to the dictionary or i could have found nothing. My question is how i should populate the dictionary. Should i use a function that returns a key value pair if found and otherwise an empty one contained within a dictionary.Add(function()) call? i dont want to add empty key/value pairs to the dictionary so im not sure how the return call for that function would work. Or should i pass the dictionary to the function and add to it if needed? like

function(dictionary) 
{ if (pair found) {dictionary.add(pair)}}
like image 636
John Baum Avatar asked Mar 27 '12 13:03

John Baum


People also ask

How to add a key value pair to dictionary in Python?

Let’s see how to add a key:value pair to dictionary in Python. Code #1: Using Subscript notation. This method will create a new key:value pair on a dictionary by assigning a value to that key. # Python program to add a key:value pair to dictionary. dict = {'key1':'geeks', 'key2':'for'}.

How do you add a new key to a dictionary?

Because of this, direct assignment is the primary way of adding new items to a dictionary. We can see here that we were able to easily append a new key:value pair to a dictionary by directly assigning a value to a key that didn’t yet exist.

How to add a new element to the dictionary in Python?

We add a new element to the dictionary by using a new key as a subscript and assigning it a value. Running the above code gives us the following result − The update method directly takes a key-value pair and puts it into the existing dictionary. The key value pair is the argument to the update function.

What is the difference between a dictionary and a key?

Dictionary pairs of values are kept in pairs, where one element is the key and the other is the value. Values in dictionaries can be of any type of data and can be replicated, unlike keys, which cannot be repeated and must be immutable.


1 Answers

Not sure what you ask here, but here is how I handle dictionary to either add or update a value based on a key:

string key = "some key here";
string value = "your value";
if (myDict.ContainsKey(key))
{
    myDict[key] = value;
}
else
{
    myDict.Add(key, value);
}

You can wrap this in a method if you like:

void AddOrUpdate(Dictionary<string, string> dict, string key, string value)
{
    if (dict.ContainsKey(key))
    {
        dict[key] = value;
    }
    else
    {
        dict.Add(key, value);
    }
}

//usage:
AddOrUpdate(myDict, "some key here", "your value");

You can also use the TryGetValue method but can't see any obvious advantage in this.

like image 152
Shadow Wizard Hates Omicron Avatar answered Oct 05 '22 23:10

Shadow Wizard Hates Omicron