dict.items()

Python · Dictionaries

Theory

.items() returns a view of all key-value pairs of a dictionary, each pair as a (key, value) tuple, in insertion order. Combined with tuple unpacking in a for loop, for k, v in employee.items():, it gives you both the key and its value on every iteration, which is the most common way to walk through a dictionary. For employee = {'name': 'Asha', 'salary': 65000}, the loop prints name Asha and then salary 65000.

Like keys() and values(), items() is a live view, not a copy, so it is cheap even for big dicts. It also powers handy patterns: dict comprehensions such as {k: v for k, v in d.items() if v > 0} to filter, sorted(d.items(), key=lambda kv: kv[1]) to sort by value, and dict(pairs) to build a dict back from pairs. One rule: do not add or delete keys while looping over items(); Python raises RuntimeError: dictionary changed size during iteration. Loop over list(d.items()) if you must modify.

Why we use it

Most dictionary work needs the key and the value together: printing a report, filtering by value, transforming every entry. items() gives both in one step, without a second lookup like d[k] inside the loop.

When to use it

Use items() whenever a loop needs both key and value. Use values() when you only need the values and plain for k in d when you only need keys. Iterate over a list(d.items()) copy if the loop changes the dict's size.

How it works

  1. for k, v in employee.items():
  2. Ask the view for the next (key, value) pair
  3. Is there another pair? Yes: Unpack it into k and v. No: Leave the loop.
  4. Run the loop body with k and v, e.g. print(k, v)
  5. Output: name Asha, then salary 65000

Syntax

dict.items()

Example

employee = {'name': 'Asha', 'salary': 65000}
for k, v in employee.items():
    print(k, v)

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