dict.keys() / .values()

Python · Dictionaries

Theory

.keys() returns all the keys of a dictionary and .values() returns all the values, both in insertion order. With employee = {'name': 'Asha', 'salary': 65000}, list(employee.keys()) is ['name', 'salary'] and list(employee.values()) is ['Asha', 65000]. They do not return lists; they return view objects (dict_keys and dict_values) that look into the dict live, so if you add a key later, an existing view shows it too. Wrap a view in list() when you need indexing or a snapshot.

Views are cheap: no copying happens, and you can loop over them, use len() and test membership. Key views even behave like sets, so employee.keys() & other.keys() finds common keys. Note that looping over the dict itself already gives keys (for k in employee), so .keys() is often unnecessary. Searching values with x in employee.values() is a linear scan, O(n), unlike the hashed key check x in employee, which is O(1) on average.

Why we use it

You often need just one side of a dictionary: all column names, all scores to average, every product ID. keys() and values() hand you exactly that side without manual loops.

When to use it

Use values() to aggregate (sum(d.values()), max(d.values())) and keys() for set-like comparisons between dicts. Just loop over the dict for keys. Use items() when you need both key and value together.

How it works

  1. Call employee.keys()
  2. Python creates a live view onto the dict (no copy)
  3. Need a real list (indexing, snapshot)? Yes: Wrap it: list(employee.keys()). No: Loop over the view directly.
  4. Iterate in insertion order
  5. Output: ['name', 'salary'] and ['Asha', 65000]

Syntax

dict.keys()
dict.values()

Example

employee = {'name': 'Asha', 'salary': 65000}
print(list(employee.keys()))
print(list(employee.values()))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic