#!/usr/bin/env python import re import json def slugify(title): title = title.lower() empties = re.compile('[-\\s]+') title = empties.sub('-', title) odds = re.compile('[^\w\s-]') title = odds.sub('', title) return title v = [ {'title': 'Apartment, The'}, {'title': 'Apartment, The'}, {'title': 'Apartment, The'}, {'title': 'Apartment, The'}, {'title': 'Apartment, The'}, {'title': 'Bar'}, {'title': 'Foo'}, {'title': 'Foo'}, {'title': 'Foo Bar'}, {'title': 'Foo: 3'}, {'title': 'The Foo is Strong'}, ] ov = [] for item in v: # make a base slug of the title slug = slugify(item['title']) # create regex with base slug reg = ur"^" + slug + "(-[0-9][^a-z]*)?$" print reg # find all items already loaded that have same slug with or without "-n" matches = [a for a in ov if re.match(reg, slugify(a['title']), re.MULTILINE)] if len(matches) > 0: # if we have some matching slugs add "-n" with the next index item['slug'] = "%s-%i" % (slug, len(matches)) else: # if no matches then just use the base slug item['slug'] = slug ov.append(item) print json.dumps(ov, indent=4)