FAQ: Using Dictionaries - Get All Keys


#1

This community-built FAQ covers the “Get All Keys” exercise from the lesson “Using Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Get All Keys

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Why It is not correct to do with the below code:
users = list(user_ids)

I have printed out it and it is matching with the needs.


#4

Following the instructions, the following is the expected output for print (users)

dict_keys(['pythonGuy', 'keysmithKeith', 'lyleLoop', 'teraCoder', 'samTheJavaMaam'])

whereas your code gives the following:

['pythonGuy', 'keysmithKeith', 'lyleLoop', 'teraCoder', 'samTheJavaMaam']
object.keys()

is not a proper list, per se, but a dict_keys object.


#6

maksagery , I made the same mistake, mate.

Check the following in the description:

Dictionaries also have a “ .keys() ” method that returns a “ dict_keys ” object.


#7

Thank you for the hint mtf !


#8
user_ids = {"teraCoder": 100019, "pythonGuy": 182921, "samTheJavaMaam": 123112, "lyleLoop": 102931, "keysmithKeith": 129384}

print(user_ids.keys())
print(list(user_ids.keys()))
print(list(user_ids))

result

dict_keys(['pythonGuy', 'teraCoder', 'keysmithKeith', 'samTheJavaMaam', 'lyleLoop'])
['pythonGuy', 'teraCoder', 'keysmithKeith', 'samTheJavaMaam', 'lyleLoop']
['pythonGuy', 'teraCoder', 'keysmithKeith', 'samTheJavaMaam', 'lyleLoop']

can someone explain what the different for this code…?
i mean, which one most suitable to use or most common…?

thanks


#9

In what way is the list sorted?
I can’t see it being sorted alphabetically or by value nor length of word.