The World’s Largest Online Community for Developers
Let be the follow function bellow. The function takes the elements line by line from a file and adds it to a dictionary (each line has a key and a value).
with open("archive.txt") as f: for line in f: (key, val) = line.split() dictionary[key] = val
How to prevent the insertion of a duplicate key or item?
If you want to always keep the first value associated with a given key, a simple trick is to use
setdefault to set the value only if the key doesn't already exist:
with open("archive.txt") as f: for line in f: key, val = line.split() dictionary.setdefault(key, val)
dictionary.setdefault(key, val) is essentially equivalent to the somewhat more verbose:
if key not in dictionary: dictionary[key] = val
though (at least on CPython, though not necessarily all alternate interpreters) the former is atomic (when the key is a built-in type, e.g.
str in this case), while the latter isn't (two threads could both perform the test, find no such key, then both insert, in arbitrary order, and both threads would believe they set the value, even though only the second to write would win).
If keeping the last value is what you want, then your original code is already fine;
dicts only store one copy of a key, so replacing the associated value if it's reassigned, so your original code only keeps the last value already.
Check if the dictionary contains the key before overwriting a key/value that's already there
with open("archive.txt") as f: for line in f: (key, val) = line.split() if key not in dictionary: dictionary[key] = val