I’m using Python 2.7 and want to create a choice in a Django form. I have a dictionary of the items I want in the choice drop-down but want to know the most pythonic way to handle this.
Here is my solution:
my_dict = {
'AL' : 'Alabama',
'AK' : 'Alaska',
etc...
}
my_list = []
for value in (my_dict):
my_list.append('({0}, {1})'.format(value, gateways.get(value)))
my_tuple = '({0})'.format(','.join(my_list))
print my_tuple
(('AL', 'Alabama'),('AK', 'Alaska'),etc...)
This will work, but it didn’t look very elegant to me. Can someone suggest a better solution?
In fact, you do not necessarily need a tuple for create a choice, quite a list:
For tuple: