James
James

Reputation: 63

Python convert dictionary into tuple

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

Answers (3)

2rs2ts
2rs2ts

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

intuited
intuited

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

Spike
Spike

Reputation: 5130

tuples = genreDictionary.items()

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

Upvotes: 7

Related Questions