diff --git a/05_load_json_without_dupes.py b/05_load_json_without_dupes.py new file mode 100644 index 0000000..5b767a2 --- /dev/null +++ b/05_load_json_without_dupes.py @@ -0,0 +1,11 @@ +import json + +def dict_raise_on_duplicates(ordered_pairs): + """reject duplicate keys""" + my_dict = dict() + for key, values in ordered_pairs: + if key in my_dict: + raise ValueError("Duplicate key: {}".format(key,)) + else: + my_dict[key] = values + return my_dict \ No newline at end of file diff --git a/readme.md b/readme.md index 94eeac3..0dae7ad 100644 --- a/readme.md +++ b/readme.md @@ -4,3 +4,4 @@ 1. **02_find_all_links.py**: get all links from a webpage 1. **03_simple_twitter_manager.py**: accessing the Twitter API, example functions 3. **04_rename_with_slice.py**: rename group of files, within a single directory, using slice +4. **05_load_json_without_dupes.py: load json, convert to dict, raise error if there is a duplicate key