Check out the setdefault() dictionary method...from the Library Reference: setdefault() is like get(), except that if k is missing, x is both returned and inserted into the dictionary as the value of k.