← Back to Blog

Getting Information From a Dict

The problem...

You have a dict. But you don't always know what's in it.

Is a specific key there? How many entries does it have? What are all the keys?

You need tools to look inside — without modifying anything.

The idea!

Python gives you a set of methods and operators that read a dict and report back.

You already saw some of them in the previous lesson. Here they're all together.

Checking if a key exists

Use in to check if a key is in the dict:

soldier = {"name": "Raven", "score": 85, "active": True}
print("name" in soldier)
print("rank" in soldier)

Output:

True
False

Use not in to check the opposite:

print("rank" not in soldier)

Output → True

Note: in checks keys only — not values.

How many entries

len() returns the number of key-value pairs:

soldier = {"name": "Raven", "score": 85, "active": True}
print(len(soldier))

Output → 3

Viewing keys, values, and pairs

soldier = {"name": "Raven", "score": 85, "active": True}
print(soldier.keys())
print(soldier.values())
print(soldier.items())

Output:

dict_keys(['name', 'score', 'active'])
dict_values(['Raven', 85, True])
dict_items([('name', 'Raven'), ('score', 85), ('active', True)])

These return views — not lists. They reflect the current state of the dict.

If the dict changes, the view updates automatically.

Convert to a list when you need list behavior:

print(list(soldier.keys()))     # ['name', 'score', 'active']

Checking a value — not a key

in checks keys. To check if a value exists anywhere in the dict, use in soldier.values():

soldier = {"name": "Raven", "score": 85}
print(85 in soldier.values())
print(99 in soldier.values())

Output:

True
False

Copying a dict

copy() returns a new dict with the same key-value pairs:

soldier = {"name": "Raven", "score": 85}
backup = soldier.copy()
backup["score"] = 99
print(soldier)
print(backup)

Output:

{'name': 'Raven', 'score': 85}
{'name': 'Raven', 'score': 99}

Same rule as lists: assigning one dict to another variable doesn't copy it. Changes go both ways. Use copy() when you need independence.

What's really happening

None of these modify the dict. They read it and return information.

in and len() are built-in — they work on the dict from outside.

keys(), values(), items(), copy() are dict methods — you call them on the dict itself.

Heads up!

  • in checks keys only — use in d.values() to check values
  • keys(), values(), items() return views — not lists
  • Views update automatically when the dict changes
  • backup = d is not a copy — use d.copy()

The mindset shift

Stop thinking: "I need to loop through everything to find what I need."

Start thinking: "Python already has tools for the most common questions — use them."

What you should understand now

  • Use in and not in to check if a key exists
  • len() returns the number of key-value pairs
  • keys(), values(), items() give you views of the dict
  • Use in d.values() to check if a value exists
  • copy() creates an independent copy of the dict
[ login to bookmark ] // copied! 18 views · 2 min
// resources
Code Example dict_inspection.py
← prev How to Modify a Dict next → Going Through Every Key and Value
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.