temp_list = CategoryItem.objects.filter(category__in=categories_list).order_by('category') big_list = [ 0 for x in range(len(categories_list))] for x in range(len(temp_list)): for y in range(len(categories_list)): if str(temp_list[x].category) == str(categories_list[y].name): tempIndex = y else: tempIndex = -1 if tempIndex > -1: big_list[tempIndex] = big_list[tempIndex] + 1 counter = 0 category_dict = {} for category in categories_list: category_dict[category.name] = big_list[counter] print category_dict[category.name] counter = counter + 1