Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jassim Alhatem
Jassim Alhatem
20,859 Points

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 67,987 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!!