Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Print Dictionary Without One Key or Multiple Keys
1
Python Print Dictionary Without One Key or Multiple Keys

5/5 – (1 vote)

The most Pythonic way to print a dictionary except for one or multiple keys is to filter it using dictionary comprehension and pass the filtered dictionary into the
Code:
print()
function.

There are multiple ways to accomplish this and I’ll show you the best ones in this tutorial. Let’s get started! [Image: 1f680.png]

[Image: 1f449.png] Recommended Tutorial: How to Filter a Dictionary in Python

Method 1: Dictionary Comprehension

[Image: image-55-1024x683.png]

Say, you have one or more keys stored in a variable
Code:
ignore_keys
that may be a list or a set for efficiency reasons.

Create a filtered dictionary without one or multiple keys using the dictionary comprehension
Code:
{k:v for k,v in my_dict.items() if k not in ignore_keys}
that iterates over the original dictionary’s key-value pairs and confirms for each key that it doesn’t belong to the ones that should be ignored.

Here’s a minimal example:

Code:
ignore_keys = {'x', 'y'} my_dict = {'x': 1, 'y': 2, 'z': 3} filtered_dict = {k:v for k,v in my_dict.items() if k not in ignore_keys} print(filtered_dict) # {'z': 3}

The
Code:
dict.items()
method creates an iterable of key-value pairs over which we can iterate.

The membership operator
Code:
k not in ignore_keys
tests if a given key doesn’t belong to the set.

[Image: 1f4a1.png] The runtime complexity of the membership check is constant O(1) if you use a set for the
Code:
ignore_keys
data structure. It would be linear O(n) in the number of elements if you used a list which is not a good idea for that reason.

Note that you can also use this approach to print a dictionary except a single key by putting only one key into the ignore list.

[Image: 1f449.png] Recommended Tutorial: Dictionary Comprehension in Python

Method 2: Simple For Loop with If Condition

[Image: image-56-1024x683.png]

A not-so-Pythonic but reasonably readable way to print a dict without one or multiple keys is to use a simple
Code:
for
loop with
Code:
if
condition to avoid all keys in the ignore list.

Here’s an example using three lines and directly printing the key-value pairs:

Code:
ignore_keys = {'x', 'y'} my_dict = {'x': 1, 'y': 2, 'z': 3} for k, v in my_dict.items(): if k not in ignore_keys: print(k, v)

The output:

Code:
z 3

Of course, you can modify the output to your own needs. See the customizations of the built-in
Code:
print()
function and its awesome arguments:

[Image: 1f449.png] Recommended Tutorial: Python
Code:
print()
and Separator and End Arguments


My Recommendation – Use This Method!

I could have listed many more ways to solve this problem of printing a dict except one or more keys.

I have seen super inefficient ways proposed on forums that use
Code:
exclude_keys
that are list types.

I have also seen elaborate schemes to use set difference operations or more.

But I don’t recommend anything else than dict comprehension if you want to create a filtered dictionary object first and the simple
Code:
for
loop
if you want to print on the fly.

That’s it. [Image: 1f44c.png]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: