Python converts a dictionary to a tuple

How to convert a dictionary to a tuple? Below is my dynamic dictionary.

genreOptions = GenreGuideServiceProxy.get_all_genres(); genreDictionary = {}; for genre in genreOptions: genreDictionary[genre.name] = genre.name; 
+6
python django-forms
source share
3 answers
 tuples = genreDictionary.items() 

See http://docs.python.org/library/stdtypes.html#dict.items

+6
source share

Do you want to make pairs (key, value)? Here is the code for generating a list (key, value) of tuples ...

 thelist = [(key, genreOptions[key]) for key in genreOptions] 

Ahh, I see that a more efficient answer is higher ...

 thelist = genreDictionary.items() 

But I want to include an example of understanding the list anyway :)

+2
source share

Check out dict.values , dict.items , and dict.iteritems for various ways to do this.

dict.values and dict.items return lists; dict.itervalues and dict.iteritems return iterators.

+1
source share

All Articles