Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I check whether particular key is present in Dictionary before accessing it?

Should I check whether particular key is present in Dictionary if I am sure it will be added in dictionary by the time I reach the code to access it?

There are two ways I can access the value in dictionary

  1. checking ContainsKey method. If it returns true then I access using indexer [key] of dictionary object.

or

  1. TryGetValue which will return true or false as well as return value through out parameter.

(2nd will perform better than 1st if I want to get value. Benchmark.)

However if I am sure that the function which is accessing global dictionary will surely have the key then should I still check using TryGetValue or without checking I should use indexer[].

Or I should never assume that and always check?

like image 841
Sun Avatar asked Jul 21 '09 08:07

Sun


People also ask

How to check if a key is present in a dictionary?

keys () method returns a list of all the available keys in the dictionary. With the Inbuilt method keys (), use if statement and the ‘in’ operator to check if the key is present in the dictionary or not. This method simply uses if statement to check whether the given key exist in the dictionary.

How to check if a key exists in a dictionary in Python?

Given a dictionary in Python, write a Python program to check whether a given key already exists in a dictionary. If present, print “Present” and the value of the key. Otherwise print “Not present”. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

How to check if a key is present or not in Python?

If present, print “Present” and the value of the key. Otherwise print “Not present”. Examples: keys() method returns a list of all the available keys in the dictionary. With the Inbuilt method keys(), use if statement and the ‘in’ operator to check if the key is present in the dictionary or not. # given key already exists in a dictionary.

How to access key-value in dictionary in Python?

Python | Accessing Key-value in Dictionary. Dictionary is quite a useful data structure in programming that is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let’s discuss various ways of accessing all the keys along with their values in Python Dictionary. Method #1 : Using in operator.


2 Answers

Use the indexer if the key is meant to be present - if it's not present, it will throw an appropriate exception, which is the right behaviour if the absence of the key indicates a bug.

If it's valid for the key not to be present, use TryGetValue instead and react accordingly.

(Also apply Marc's advice about accessing a shared dictionary safely.)

like image 165
Jon Skeet Avatar answered Oct 13 '22 11:10

Jon Skeet


If the dictionary is global (static/shared), you should be synchronizing access to it (this is important; otherwise you can corrupt it).

Even if your thread is only reading data, it needs to respect the locks of other threads that might be editing it.

However; if you are sure that the item is there, the indexer should be fine:

Foo foo;
lock(syncLock) {
    foo = data[key];
}
// use foo...

Otherwise, a useful pattern is to check and add in the same lock:

Foo foo;
lock(syncLock) {
    if(!data.TryGetValue(key, out foo)) {
        foo = new Foo(key);
        data.Add(key, foo);
    }
}
// use foo...

Here we only add the item if it wasn't there... but inside the same lock.

like image 40
Marc Gravell Avatar answered Oct 13 '22 11:10

Marc Gravell