Idiomatic dictionaries

get - default value of a non existing key while accessing

Especially handy if you're unsure about the presence of a key.

In [ ]:
my_dict = {'a': 1, 'b': 2, 'c': 3}

Don't do it like this.

In [ ]:
if 'g' in my_dict:
    value = my_dict['g']
else:
    value = 'some default value'
print(value)

Or like this.

In [ ]:
try:
    value = my_dict['g']
except KeyError:
    value = 'some default value'
print(value)

Do it like this!

In [ ]:
value = my_dict.get('g', 'some default value')
print(value)

Note that if you don't provide the default value for get, the return value will be None if the key is not present in the dictionary

In [ ]:
value = my_dict.get('g')
print(value is None)

setdefault - same as get but also sets the value if not present

Don't do it like this.

In [ ]:
my_dict = {'a': 1, 'b': 2, 'c': 3}

key = 'g'
if key in my_dict:
    value = my_dict[key]
else:
    value = 'some default value'
    my_dict[key] = value
    
print(value)
print(my_dict)

Let's do it like this!

In [ ]:
my_dict = {'a': 1, 'b': 2, 'c': 3}

key = 'g'
value = my_dict.setdefault(key, 'some default value')

print(value)
print(my_dict)

Comprehensions

Let's say we have a collection of numbers and we want to store those as a dictionary where the number is key and it's square is the value.

In [ ]:
numbers = (1, 5, 10)

Don't do it like this.

In [ ]:
squares = {}
for num in numbers:
    squares[num] = num**2
print(squares)

Do it like this!

In [ ]:
squares = {num: num**2 for num in numbers}
print(squares)

Another example

In [ ]:
keys = ('a', 'b', 'c')
values = [True, 100, 'John Doe']

Don't do it like this.

In [ ]:
my_dict = {}
for idx, key in enumerate(keys):
    my_dict[key] = values[idx]
print(my_dict)

Do it like this!

In [ ]:
my_dict = {k: v for k, v in zip(keys, values)}
print(my_dict)

# Or even like this:
my_dict2 = dict(zip(keys, values))

assert my_dict2 == my_dict

Looping

In [ ]:
my_dict = {'age': 83, 'is gangster': True, 'name': 'John Doe'}

Don't do it like this.

In [ ]:
for key in my_dict:
    val = my_dict[key]
    print('key: {:15s} value: {}'.format(key, val))

Do it like this!

In [ ]:
for key, val in my_dict.items():
    print('key: {:15s} value: {}'.format(key, val))