On Sun, 14 Mar 2010 10:06:49 am Ray Parrish wrote: > Hello, > > I am stuck on the following - > > # Define the Dates{} > dictionary structure as a dictionary > # containing two dictionaries, > each of which contains a list. > Dates = > {Today:{ThisIPAddress:[]}, > Tomorrow:{ThisIPAddress:[]}} > > How do I pass this declaration empty values for > Today, Tomorrow, and ThisIPAddress to initially > clare it?
You don't. Once you create a key, you can't modify it. So if you create an empty value for Today etc., it stays empty. > The idea behind the structure is to sort through a > server log that contains entries for two dates, [...] Just work out the dates before hand, and populate the dictionary that way: today = "2010-03-13" tomorrow = "2010-03-14" thisIP = "123.456.789.123" entries = {today: {thisIP: []}, tomorrow: {thisIP: []}} Or don't pre-populate the dict at all. entries = {} for line in logfile: # Process the line to get a date, an IP address, and visitor date = ... address = ... visitor = ... entry = entries.get(date, {}) x = entry.get(address, {}) x.append(visitor) entry[address] = x entries[date] = entry The trick is to use get to look up the dictionary: entries.get(date, {}) looks up date in entries, and if it isn't found, it returns an empty dict {} instead of failing. Similarly for looking up the IP address. -- Steven D'Aprano _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor