6

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;
1
  • How do you want your tuple to look? You know about genreDictionary.items() method, right? Commented Apr 5, 2011 at 22:01

3 Answers 3

7
tuples = genreDictionary.items()

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works I just had to modify it a little. tuple(genreDictionary.items());
3

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 :)

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.