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

Need help.

So, I don't know why this wasn't working for me. but I've tried many ways.

def dicts():
    tuple_dict = ({"name": "jassim", 
               "num": 200,
               "siblings":{"name":"noor",
                           "name2":"Jacob"}
              })

I want it to print out the keys at first then the values. Separate lines.

It's probably an easy answer but I'm bad with def, So thank you.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,455 Points

Hi Jassim, using the methods .keys() will get the keys of a dictionary, .values() will get the dictionary values, and .items() will get the key / value pairs returned as a tuple.

The tuple_dict in your code is actually just a dict. Wrapping an object in parens will not make it a tuple. If you use a comma after the first item then a tuple is created: (item, )

So for a given dictionary you can use:

for key in some_dict.keys():
    print(key)
for value in some_dict.values():
    print(value)
for key, value in some_dict.items():
   print(key, value)

Post back if you need any more help. Good luck!!

Thank you, Chris!