Reputation: 63
How do I convert a dictionary into a tuple? Below is my dynamic dictionary.
genreOptions = GenreGuideServiceProxy.get_all_genres();
genreDictionary = {};
for genre in genreOptions:
genreDictionary[genre.name] = genre.name;
Upvotes: 6
Views: 9328
Reputation: 11066
Do you want to make (key, value) pairs? Here is code to generate a list of (key, value) tuples...
thelist = [(key, genreOptions[key]) for key in genreOptions]
Ahh I see there is a more efficient answer above...
thelist = genreDictionary.items()
But I want to include the list comprehension example anyways :)
Upvotes: 3
Reputation: 24054
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.
Upvotes: 1
Reputation: 5130
tuples = genreDictionary.items()
See http://docs.python.org/library/stdtypes.html#dict.items
Upvotes: 7