Using dict.setdefault in Python

Photo by Christina Morillo on Pexels.com

For years, I’ve been using the following snippet in the solution for one of my interview questions:

anagrams = dict()

with open(WORDS_PATH) as f:
    for line in f:
    key = sort(line.strip())
    if key not in anagrams:
        anagrams[key] = list()
        anagrams[key].append(line.strip())
    else:
        anagrams[key].append(line.strip())

Recently, I learned to use the dict.setdefault function to further optimize it, and the end result looks like the following:

anagrams = dict()

with open(WORDS_PATH) as f:
    for line in f:
        key = sort(line.strip())
        anagrams.setdefault(key, []).append(line.strip())

Get the list of anagrams for key, or set it to [] if not found; setdefault returns the value, so it can be updated without requiring a second search. In other words, the end result of this line…

anagrams.setdefault(key, []).append(line.strip())

…is the same as running…

if key not in anagrams:
    anagrams[key] = list()
    anagrams[key].append(line.strip())
else:
    anagrams[key].append(line.strip())

…except that the latter code performs at least two searches for key – three if it’s not found – while setdefault does it all with a single lookup.

JavaScript Object Literal Shorthand with ECMAScript 2015

Photo by Markus Spiske on Pexels.com

Last Friday, I hit an ESLint error as below:

335:13  error  Expected property shorthand        object-shorthand

Per the “object-shorthand” rule’s documentation, there is a syntactic sugar for defining object literal methods and properties in the ECMAScript 2015 (ES6).

Quite often, when declaring an object literal, property values are stored in variables whose names are equal to the property names. For example:

const timeout = 30000;

const options = { timeout: timeout };

There is a shorthand for this situation:

const timeout = 30000;

const options = { timeout };

References