Concepts / Dictionary Methods and Built-in Functions

Dictionary Methods and Built-in Functions

The items() method converts a dictionary into a list of tuples, where each tuple contains one key-value pair.

  • Programming

A Dictionary Becomes Pairs

A dictionary stores related keys and values together. Sometimes, however, you need to handle each key and value as one separate unit. The items() method provides that view of the dictionary: each dictionary entry is represented as a two-element tuple containing one key-value pair. When the result is converted with list(d.items()), the result is an actual list whose elements are those tuples.

What do you think happens?

Suppose dictionary keys were added in the order b, a, c. What order should the key-value tuples have after items() is applied?

  • Alphabetical order: a, b, c
  • Insertion order: b, a, c
  • Reverse alphabetical order: c, b, a
Reveal answer

Answer: Insertion order: b, a, c

The tuples are returned in the order in which the keys were added to the dictionary, not in alphabetical order.

Mapping Entries to Tuples

applyentryentryentryDictionarythree key-value pairsitems()extract pairsTuple 1key, valueTuple 2key, valueTuple 3key, value
How does each dictionary key-value pair become a separate tuple when items() is applied?

The transformation preserves the relationship between every key and its value. A dictionary entry does not become two unrelated values. Instead, the key and value stay together inside one tuple. If the dictionary has three entries, the list created with list(d.items()) has three tuple elements, one for each entry.

Inside Each Tuple

containscontainscontainsList of pairstuple elementsTupleexactly two elementsKeyindex 0Valueindex 1
What contains what in the result: the collection of pairs, each tuple, and the key and value inside each tuple?

Each tuple returned by items() contains exactly two elements. The key is at index 0, and the value is at index 1. You can therefore use tuple index notation when you need one part of a pair. The tuple is immutable, meaning that once it has been created, its contents cannot be changed. This fixed structure keeps each key-value relationship together.

The essential pattern is collection of tuples, with each tuple structured as key at index 0 and value at index 1.

Insertion Order

position 1position 2position 3bfirst insertedTuple sequenceb, a, casecond insertedcthird inserted
In what order do key-value pairs appear after items(), and how does that order relate to the order in which entries were inserted?

The order of the tuples is the dictionary's insertion order: the order in which the keys were added. It is not alphabetical order. For example, if the keys b, a, and c were added in that sequence, the resulting tuples appear in the order b, a, c. If alphabetical order is required, the resulting list must be sorted separately.

A dictionary whose entries are added in the order mango, apple, zebra produces pairs in that same order. Although apple comes before mango alphabetically, it appears second because it was added second.

A Worked Conversion

Converting Three Entries

A dictionary contains three key-value pairs. Use items() and list() to represent those entries as a list of tuples.

Start with the dictionary: The dictionary contains three key-value relationships. Each relationship consists of one key and its associated value.

Apply items(): The dictionary entries are exposed as pairs. Each pair contains exactly two elements: its key followed by its value.

Create the list: Use list(d.items()) to convert the dict_items object into an actual list that can be stored, printed, or passed to a function.

Read each tuple: For every tuple, index 0 identifies the key and index 1 identifies the value. The tuples remain in insertion order.

The final structure is a list of immutable two-element tuples, with one tuple for each dictionary entry.

This conversion changes the container used to organize the data while preserving the key-value relationships. The dictionary representation emphasizes lookup by key. The tuple-based list representation emphasizes a sequence of fixed pairs that can be iterated over, stored, printed, or passed to a function.

Iteration with Pairs

items() is especially useful when an operation needs both the key and the value at the same time. Instead of handling a key separately from its value, iteration can move through the sequence of two-element tuples. Each tuple can be unpacked into two variables, one for the key and one for the value. The key-value relationship stays intact throughout the iteration.

  • First, items() represents each dictionary entry as one key-value tuple.
  • Next, iteration reaches one tuple at a time in insertion order.
  • Within a tuple, index 0 identifies the key and index 1 identifies the value.
  • The fixed two-element structure makes each pair suitable for direct unpacking during iteration.

Mistakes to Avoid

  • Expecting alphabetical order

    items() follows insertion order rather than alphabetical order.

    Fix: Expect b, a, c when those keys were added in that order. Sort the list separately if alphabetical order is needed.

  • Treating one tuple as more than a key-value pair

    Each tuple contains exactly two elements: the key and the value.

    Fix: Use index 0 for the key and index 1 for the value.

  • Trying to change a returned tuple

    The tuples returned by items() are immutable.

    Fix: Treat each tuple as a fixed representation of one key-value relationship.

  • Forgetting the list conversion when an actual list is required

    items() provides the dictionary items, while list(d.items()) creates the actual list.

    Fix: Use list(d.items()) when a list of tuple pairs is required.

Practice Check

EASY

A dictionary receives the keys zebra, apple, and mango in that order. Describe the order of the tuples produced after converting its items to a list. Then describe which element of each tuple contains the key and which contains the value.

Hints
  • Use insertion order, not alphabetical order.
  • Each tuple has exactly two elements.
  • The key is at index 0 and the value is at index 1.

The expected reasoning is that the tuples remain in the order zebra, apple, mango because that is the insertion order. For every tuple, the key occupies index 0 and its associated value occupies index 1. The result is a list of fixed key-value pairs when list(d.items()) is used.

Key Takeaways

  1. items() represents dictionary entries as key-value pairs.
  2. Each pair is an immutable tuple containing exactly two elements: the key at index 0 and the value at index 1.
  3. The tuples follow dictionary insertion order, not alphabetical order.
  4. Use list(d.items()) when an actual list of tuple pairs is needed for storage, printing, passing to functions, or iteration.

Key Takeaways

  • The items() method exposes dictionary entries as key-value pairs.
  • After list(d.items()), each dictionary entry is represented by one immutable two-element tuple.
  • The key is at tuple index 0 and the value is at tuple index 1.
  • The tuple sequence follows insertion order rather than alphabetical order.
  • This tuple-based format is useful for iteration, storage, printing, and passing dictionary data to functions.