StudyToCert

All certifications / PCEP / Lessons

PCEP – Certified Entry-Level Python Programmer PCEP-30-02 · Domain 3: Data collections: tuples, dictionaries, lists and strings

Iterating dictionaries with keys(), values() and items(); checking whether a key exists

▶ Watch the overview video

Last reviewed September 30, 2026 · Leer en español

Once data is in a dictionary, you will often need to walk through all of it: print a report, total the values, or find the largest entry. Dictionaries give you three views of their contents. d.keys() returns the keys, d.values() returns the values, and d.items() returns the key-value pairs as two-item tuples. These are view objects, not lists: they reflect later changes to the dictionary, and you can loop over them, test membership in them or convert them with list(). Printing one shows something like dict_keys(['a', 'b']), and views cannot be indexed, so d.keys()[0] raises TypeError; use list(d.keys())[0] if you really need a position.

Free account

Keep reading for free

Create a free StudyToCert account to read the rest of this lesson: 7 more sections, 6 key terms, a real-world example, an exam tip and self-check questions. Every lesson, lab and practice test is free with an account.

Sign up free Log in

Study PCEP for free
A week-by-week plan with every lesson, quizzes, checkpoint tests, a practice exam and hands-on labs.
Open the PCEP study plan