LaVOZs

The World’s Largest Online Community for Developers

'; How to prevent a function from adding repeated keys to a dictionary in python - LavOzs.Com

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

Example: Let be the following .txt file:

aaa 111
bbb 222
aaa 333
ccc 444

Insert in the following dict:

dictionary = ['aaa':111, 'bbb':222, 'aaa':333, 'ccc':444]

however, I would like to prevent repeated keys from being inserted, so the end result should be:

dictionary = ['aaa':111, 'bbb':222, 'ccc':444]

How to prevent the insertion of a duplicate key?

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
Related
How do I merge two dictionaries in a single expression?
How do I sort a list of dictionaries by a value of the dictionary?
Calling an external command from Python
How to get the current time in Python
How do I sort a dictionary by value?
How to make a chain of function decorators?
How can I add new keys to a dictionary?
Check if a given key already exists in a dictionary
Delete an element from a dictionary
How to remove a key from a Python dictionary?