1

Whenever I receive a new url, I try to add that in my dictionary, along with the current time. However, when I use the update() method, it replaces original values with the new values I added, so that the only thing in the dictionary now are the new values (and not the old ones).

Here is a shorter version of my code:

    if domain not in lst:
        lst.append(domain)
        domaindict = {} 
        listofdomains.append(domaindict)
        domaindict.update({domain:datetime.now().strftime('%m/%d/%Y %H:%M:%S')})
    if domain in lst:
        domindex = lst.index(domain) 
        listofdomains[domindex].update({domain:datetime.now().strftime('%m/%d/%Y %H:%M:%S')})

lst is the list of domain names so far, while listofdomains is the list that contains all the dictionaries of the separate domains (each dictionary has the domain name plus the time).

When I try to print listofdomains:

print(listofdomains)

It only prints out the newly added domain and urls in the dictionaries. I also tried to use other methods to update a dictionary, as detailed in the answers to this question, but my dictionaries are still not functioning properly.

Why did the original key/value pairs dissapear?

Rebecca Bibye
  • 190
  • 2
  • 18
  • 1
    Because you always use the same `domain` key, so you just replace the value. You can't have duplicate keys in a dict. You should probably reconsider your data structure - maintaining two parallel lists is probably not a very good idea... – Thierry Lathuille Dec 07 '20 at 18:50
  • According to documentation, the dict.update method updates the contents of the dictionary with new data. Thus, the dictionary only contains the last updated value. If you want produce a list of entries, you need to add to the dictionary value not update based on the key. – itprorh66 Dec 07 '20 at 18:52
  • @itprorh66 how do I achieve that then? – Rebecca Bibye Dec 07 '20 at 18:55
  • @ThierryLathuille Thank you so much!!!!! I finally figured out a solution after you told me that; switching the keys and values so that the new keys don't repeat. Thank you!!! – Rebecca Bibye Dec 07 '20 at 19:01
  • That all depends on what you want to do, currently you are creating a dictionary for each domain with a key defined as the string determined by ```domain:datetime.now().strftime('%m/%d/%Y %H:%M:%S')``` with no value. and listofDomains contains a list of dicts each with a single timestring key. What is it you are trying to keep track of and later access? – itprorh66 Dec 07 '20 at 19:07
  • I'm trying to keep track of the domains I visited and record when I visited them and for how long. – Rebecca Bibye Dec 07 '20 at 19:09

1 Answers1

3

The simplest structure would probably be a dict of lists:

data = {domain1:[time1, time2, ...], domain2:[...] ...}

You can build it simply using a defaultdict that creates empty lists on the fly when needed. Your code would be:

from collections import defaultdict

data = defaultdict(list)

and your whole code becomes simply:

data[domain].append(datetime.now().strftime('%m/%d/%Y %H:%M:%S'))
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50