Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Tuples Introduction To Tuples

Using a dictionary as an argument when calling tuple()

my_tuple = tuple({'ny': {'yankees', 'mets'}, 'la': 'dodgers'}) results in ('ny', 'la') when I run this in the Python Shell. Why is this result the result that comes back? Also, how can I insert code snippets when asking questions?

1 Answer

Steven Parker
Steven Parker
229,786 Points

Perhaps this will do what you want:

my_tuple = tuple({'ny': {'yankees', 'mets'}, 'la': 'dodgers'}.items())

If that's not it, please indicate what you expect to get from the conversion.

And for posting code like I have done here, just copy/paste and use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Thanks for responding Steven. When I watched the video Introduction to Tuples, Kenneth used a list for an argument within the tuple function such as in tuple([1, 2, 3]) so I just wondered what would happen if a dictionary was passed instead as in my_tuple = tuple([1, 2, 3]) which returns (1, 2, 3).

I went into the REPL in workspaces by typing just python and entered my_tuple = tuple({'ny': {'yankees', 'mets'}, 'la': 'dodgers'}) and was just wondering why when I enter my_tuple again in the REPL that I get a result of ('ny', 'la') that is just a tuple of the keys?

Steven Parker
Steven Parker
229,786 Points

Yes, by default you get just the keys. Use the ".items()" method to get the values as well.