Skip to content

Instantly share code, notes, and snippets.

@violetcode
Created April 20, 2012 04:31
Show Gist options
  • Save violetcode/2426014 to your computer and use it in GitHub Desktop.
Save violetcode/2426014 to your computer and use it in GitHub Desktop.
Alphabetizer Helper Thingy
def alphabetizer(names):
"""
Takes a list as a parameter, and sorts them into alphabetical order
and groups them by the first letter.
"""
result = []
temp_dict = {}
for name in names:
first_letter = name[0].upper()
if first_letter not in temp_dict:
temp_dict[first_letter] = []
temp_dict[first_letter].append(name)
for item, val in temp_dict.iteritems():
val.sort()
result.append({'letter': item, 'list': val})
return result
"""
SAMPLE TEMPLATE
some_dict is the data structure returned by the alphabetizer method
<ul>
{% for item in some_dict|dictsort:"letter" %}
<li>{{item.letter}}</li>
<ul>
{% for val in item.list %}
<li>{{val}}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment