0Pricing
Python Academy · Lesson

Dictionary Methods

Use get, keys, values, items, update, and pop.

Dictionary Methods is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Python dicts have a rich set of methods for merging, transforming, and safely accessing data.

keys(), values(), items()

These return view objects that reflect changes to the dict. Convert to list if you need a snapshot.
d = {'a': 1, 'b': 2}
print(list(d.keys()))
print(list(d.values()))
print(list(d.items()))

update()

d.update({'c': 3, 'd': 4}) merges another dict in. Existing keys are overwritten.
d = {'a': 1}
d.update({'b': 2, 'a': 99})
print(d)

pop() with Default

d.pop('key', default) removes and returns the value. If the key is absent and no default is given, raises KeyError.
d = {'a': 1, 'b': 2}
val = d.pop('a')
print(val, d)

popitem()

d.popitem() removes and returns the last inserted (key, value) pair. Raises KeyError on empty dict.
d = {'a': 1, 'b': 2}
print(d.popitem())
print(d)

setdefault()

d.setdefault('key', default) inserts the default if key is absent and returns the value. Avoids repeated get + set.
d = {}
d.setdefault('count', 0)
d['count'] += 1
print(d)

Merging with | (Python 3.9+)

d1 | d2 returns a new merged dict. d1 |= d2 updates d1 in place. Cleaner than update().
d1 = {'a': 1}
d2 = {'b': 2}
print(d1 | d2)

copy() and deepcopy()

d.copy() creates a shallow copy. For nested dicts, use copy.deepcopy(d) to avoid shared references.
import copy
d = {'a': [1,2]}
deep = copy.deepcopy(d)
deep['a'].append(3)
print(d, deep)

Counter from collections

from collections import Counter; Counter('banana') counts character frequencies automatically.
from collections import Counter
c = Counter('banana')
print(c)

defaultdict

from collections import defaultdict; d = defaultdict(list) auto-creates an empty list for missing keys.
from collections import defaultdict
d = defaultdict(list)
d['a'].append(1)
print(d)

OrderedDict (legacy note)

In Python 3.7+ regular dicts preserve insertion order. OrderedDict is still useful for move_to_end() and order-sensitive equality.
from collections import OrderedDict
od = OrderedDict({'a':1,'b':2})
od.move_to_end('a')
print(list(od))

Quick Check

What does d.setdefault('key', 0) do if 'key' is not in d?

Recap

Dict methods: update, pop/popitem, setdefault, copy/deepcopy. Use Counter for frequency, defaultdict to avoid missing-key checks, | for merging (3.9+).

Keep Going

Great work! Move on to the next lesson to continue building your skills.

Frequently asked questions

Is the “Dictionary Methods” lesson free?

Yes — the full text of “Dictionary Methods” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Dictionary Methods”?

Use get, keys, values, items, update, and pop. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Dictionary Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Dictionary Basics
  2. Dictionary Methods
  3. Sets and Set Operations
  4. Dictionary Comprehensions
← Back to Python Academy