def index_with_dupes(values_list, unique_together=2, model_number_i=0, serial_number_i=1, verbosity=1):
    '''Create dict from values_list with first N values as a compound key.

    Default N (number of columns assumbed to be "unique_together") is 2.
    >>> index_with_dupes([(1,2,3), (5,6,7), (5,6,8), (2,1,3)]) == ({(1, 2): (1, 2, 3), (2, 1): (2, 1, 3), (5, 6): (5, 6, 7)}, {(5, 6): [(5, 6, 7), (5, 6, 8)]})
    True
    '''
    try:
        N = values_list.count()
    except:
        N = len(values_list)
    if verbosity > 0:
        print 'Indexing %d values_lists in a queryset or a sequence of Django model instances (database table rows).' % N
    index, dupes = {}, {}
    pbar = None
    if verbosity and N > min(1000000, max(0, 100000**(1./verbosity))):
        widgets = [pb.Counter(), '%d rows: ' % N, pb.Percentage(), ' ', pb.RotatingMarker(), ' ', pb.Bar(),' ', pb.ETA()]
        pbar = pb.ProgressBar(widgets=widgets, maxval=N).start()
    rownum = 0
    for row in values_list:
        normalized_key = [str(row[model_number_i]).strip(), str(row[serial_number_i]).strip()]
        normalized_key += [i for i in range(unique_together) if i not in (serial_number_i, model_number_i)]
        normalized_key = tuple(normalized_key)
        if normalized_key in index:
            # need to add the first nondupe before we add the dupes to the list
            if normalized_key not in dupes:
                dupes[normalized_key] = [index[normalized_key]]
            dupes[normalized_key] = dupes[normalized_key] + [row]
            if verbosity > 2:
                print 'Duplicate "unique_together" tuple found. Here are all the rows that match this key:'
                print dupes[normalized_key]
        else:
            index[normalized_key] = row 
        if pbar:
            pbar.update(rownum)
        rownum += 1
    if pbar:
        pbar.finish()
    if verbosity > 0:
        print 'Found %d duplicate model-serial pairs in the %d records or %g%%' % (len(dupes), len(index), len(dupes)*100./(len(index) or 1.))
    return index, dupes
Create dict from values_list with first N values as a compound key.

    Default N (number of columns assumbed to be "unique_together") is 2.
    >>> index_with_dupes([(1,2,3), (5,6,7), (5,6,8), (2,1,3)]) == ({(1, 2): (1, 2, 3), (2, 1): (2, 1, 3), (5, 6): (5, 6, 7)}, {(5, 6): [(5, 6, 7), (5, 6, 8)]})
    True