I am scrapping a site with destination and travel time values and adding each into dictionary. There are some keys (destination names) that are the same or are off by a few words at the end. But When I print the dictionary out, it seems it only adds one of them only. Is there anyway to go around this and have it add all of them even though they have different values?
-
You could use a `defaultdict(list)` or something, but dictionaries cannot have duplicate keys – user3483203 Jun 18 '18 at 21:12
-
Possible duplicate of [make dictionary with duplicate keys in python](https://stackoverflow.com/questions/10664856/make-dictionary-with-duplicate-keys-in-python) – user3483203 Jun 18 '18 at 21:13
-
You can only have one key of a specific value, but the value can be any Python object including a list or another dictionary. – cdarke Jun 18 '18 at 21:24
2 Answers
A python dictionary cannot have duplicate keys, period - the reason is that a python dictionary is a hash table.
If you have two destination names as keys that have different travel-time values, it sounds like you have two different destinations, which you would want to keep distinct. One approach would be to add some kind of incrementing string to the end of your keys to keep them distinct, like this:
{"Old Springs Well, Springfield, CA - 1": "2 hours",
"Old Springs Well, Springfield, CA - 2": "3.5 hours"}.
If you don't want to alter the keys at all, you could store your data as a list of tuples instead of a dictionary. With a list of tuples you lose the O(1) access time of a dictionary value when its key is known, but if your main goal is simply to record the information from the website, a list of tuples is fine.

- 110
- 1
- 4
- 9
-
thanks. some ppl were suggesting to use defaultdict, but for my purpose I needed to output the key/value pair in order. Your suggestions make more sense. However I don't want to add additional text to the end, I think the list of tuples would be better. The question I have is it seems I have to zip the two list up first. Each list containing keys and the other values to form the list tuple. Would it be more efficient to just print out the the two lists instead of the list tuple? Example: print(keylist[i] + ' ' valuelist[i]). Of course I will be outputting to spreadsheet instead – superx Jun 19 '18 at 02:06
-
1If you have the two lists in order in memory already, then a print operation like you show above is a fine approach - there is no need to waste time making a new data structure (like a list of tuples) before printing the output or piping the output to a text file. – dimachidy Jun 19 '18 at 05:02
You could use multidict. Bottle has a version of this inside of itself. This allows for duplicate dict keys.

- 3,809
- 1
- 14
- 20