Tech Off Post

Single Post Permalink

View Thread: How To Modify An Element in the Dictionary Class?
  • User profile image

    , evildictait​or wrote

    foobar[foo] = bar; // invalid - foo isn't in the collection yet.

    Incorrect. This is perfectly valid.

    , evildictait​or wrote

    foobar[foo] = bar2; // valid way of changing the element whose key is foo, but only once the element has been added in the first place.

    Again, incorrect. The key doesn't have to be present in the dictionary for assignment to work. JPeless had it correct, though either of the following are even simpler:

    runningcount[city] += 1;runningcount[city]++;

    Note, however, that these variants do require the key to be in the dictionary.