October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Get or Select Multiple Keys from a Dictionary in Python

Use a dictionary comprehension to select keys, or retrieve multiple values as a list or tuple. These examples show how each option handles missing keys and ordering.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To select several keys into a new dictionary and silently omit keys that are missing, use a dictionary comprehension: selected = {k: d[k] for k in keys if k in d}. If you need values in a list or tuple instead, choose a lookup that matches how you want missing keys handled.

Choose the result you need

These examples use the same dictionary and requested keys:

d = {"name": "Ada", "age": 36, "city": "London"}
keys = ["name", "city", "country"]
Goal Expression Result and missing-key behavior
New dictionary; skip absent keys {k: d[k] for k in keys if k in d} {'name': 'Ada', 'city': 'London'}; absent keys are omitted.
List; provide a fallback [d.get(k, None) for k in keys] ['Ada', 'London', None]; replace None with another default if desired.
List; require all keys [d[k] for k in keys] Returns values in requested-key order; raises KeyError if a key is absent.
Tuple; require all keys itemgetter(*keys)(d) Returns a tuple; raises KeyError if a key is absent.

Build a smaller dictionary

Use a comprehension when you want a new dictionary rather than just the values. This version follows the order of keys and ignores requested keys that are not in d:

selected = {k: d[k] for k in keys if k in d}
# {'name': 'Ada', 'city': 'London'}

Membership testing with k in d checks whether the key is present. The official Python 3.12 documentation for dictionary types describes membership, subscription, and get().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Return values in a list

Use a default for missing keys

Call get() for each requested key when a missing key should produce a fallback rather than an exception:

values = [d.get(k, None) for k in keys]
# ['Ada', 'London', None]

dict.get(key, default) returns the default when the key is absent. Choose a fallback that makes sense for your data; the default can be any value.

Raise an error if a key is absent

Use subscription when every requested key must exist:

values = [d[k] for k in keys]
# ['Ada', 'London']

The returned list follows the order of keys. If even one requested key is missing, subscription raises KeyError, which can help expose unexpected or incomplete input rather than quietly substituting a value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Return a tuple with itemgetter

For a tuple of values when all keys are expected to exist, use itemgetter from the standard-library operator module:

from operator import itemgetter

values = itemgetter(*keys)(d)
# ('Ada', 'London', 'Paris' would require 'country' to exist)

With multiple item names, itemgetter returns a tuple. It uses item access, so an absent key raises KeyError; use the get() comprehension if missing keys need defaults. See the Python 3.13 operator documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Select keys in dictionary order instead

If the set of desired keys matters but their requested order does not, iterate over the dictionary and test membership in a set:

wanted = {"name", "city"}
selected = {k: d[k] for k in d if k in wanted}

This builds the result in the dictionary’s key order, not the order of wanted. Dictionary iteration follows insertion order. To produce sorted keys instead, iterate over sorted(d) and apply the same membership test. The Python tutorial’s dictionary section documents key iteration and sorted-key examples.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which approach should you use?

  • Choose a dictionary comprehension when you need a subset dictionary.
  • Choose d.get(k, default) when absent keys should receive a fallback.
  • Choose d[k] or itemgetter(*keys)(d) when all keys should exist and a missing key should raise an error.
  • Use the requested-key sequence to control result order, or iterate over the dictionary when its key order should control the result.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.