I have an array filled with 5 other arrays that consists of arrays of 2 values (first a date then a count):[   [     [dt, cnt], [dt, cnt], ....   ],   [     [dt, cnt], [dt, cnt], ....   ],   [     [dt, cnt], [dt, cnt], ....   ],   [     [dt, cnt], [dt, cnt], ....   ],   [     [dt, cnt], [dt, cnt], ....   ], ]It concerns click statistics of different websites. This data needs to be converted to data to make a consistent chart with Google visualisations. So first a conversion is done in Python, then this result converted to JSON to pass to the Google libs.My current code is this:# determine min datemindate = datetime.date.maxfor dataSet in sets:    if (dataSet[0][0] < mindate):        mindate = dataSet[0][0];# fill a dictionary with all dates involveddatedict = {}for dat in daterange(mindate, today):    datedict[dat] = [dat];# fill dictionary with rest of dataarrlen = 2for dataSet in sets:    # first the values    for value in dataSet:        datedict[value[0]] = datedict[value[0]] + [value[1]];    # don't forget the missing values (use 0)    for dat in daterange(mindate, today):        if len(datedict[dat]) < arrlen:            datedict[dat] = datedict[dat] + [0]    arrlen = arrlen + 1# convert to an arraydatearr = []for dat in daterange(mindate, today):    datearr = datearr + [datedict[dat]]# convert to jsonresult = json.dumps(datearr, cls=DateTimeEncoder)(The DateTimeEncoder just gives a JavaScript-friendly datetime in the JSON)The output looks like this:[    [new Date(2008, 7, 27), 0, 5371, 1042, 69, 0],     [new Date(2008, 7, 28), 0, 5665, 1100, 89, 0],     ...]This is one of my first Python adventures, so I expect this piece of code can be improved upon easily. I want this to be shorter and more elegant. Show me the awesomeness of Python because I'm still a bit disappointed.I'm using Django, by the way.
Converting click count data to JSON
python;beginner;datetime;converting;json
List comprehensions are definitely the way to go here.  They allow you to say what you mean without getting mired in the details of looping.To find the first date, make use of the built-in min() function.defaultdict is useful for handling lookups where there may be missing keys.  For example, defaultdict(int) gives you a dictionary that reports 0 whenever you lookup a date that it doesn't know about.I assume that each of the lists within sets represents the data for one of your sites.  I recommend using site_data as a more meaningful alternative to dataSet.from collections import defaultdictimport jsondef first_date(series):    return series[0][0]start_date = min(first_date(site_data) for site_data in sets)# counts_by_site_and_date is just a more convenient version of sets, where# the [dt, cnt] pairs have been transformed into a dictionary where you can# lookup the count by date.counts_by_site_and_date = [defaultdict(int, site_data) for site_data in sets]result = json.dumps([    [date] + [site_counts[date] for site_counts in counts_by_site_and_date]        for date in daterange(start_date, today)], cls=DateTimeEncoder)