How to Print a Dictionary Without Brackets in Python?

Problem Formulation

Given a dictionary of key value pairs in Python. If you print the dictionary to the shell using print({'a': 1, 'b': 2}), the output is enclosed in curly brackets (braces) like so: {'a': 1, 'b': 2}. But you want the dictionary without brackets like so: 'a': 1, 'b': 2.

d = {'a': 1, 'b': 2}
print(d)
# What you don't want:
# {'a': 1, 'b': 2}

How to print the dictionary without enclosing curly braces?

# What you want:
# 'a': 1, 'b': 2
# or: 
# a:1 b:2

Method 1: Unpacking + List Comprehension

To print a dictionary without enclosing brackets, you can dynamically create a list of strings using list comprehension. Each string represents a mapping from key to value. You iterate over the dictionary.items() method to get the key-value tuples. For example, [str(k) + ':' + str(v) for k,v in d.items()] creates a list of 'key: value' strings.

d = {'a': 1, 'b': 2}
print(*[str(k) + ':' + str(v) for k,v in d.items()])

The output is:

a:1 b:2

The asterisk operator * is used to unpack an iterable into the argument list of a given function. You can unpack list elements into the print() function to print each of them individually. Per default, all print arguments are separated by an empty space. For example, the expression print(*my_list) will print the elements in my_list, empty-space separated, without the enclosing square brackets!

To master the basics of unpacking, feel free to check out this video on the asterisk operator:

Method 2: Unpacking + List Comprehension + Print Separator

To print a comma-separated list without enclosing square brackets, the most Pythonic way is to unpack all list values into the print() function and use the sep='\n' argument to separate the list elements with a newline character.

d = {'a': 1, 'b': 2}
print(*[str(k) + ':' + str(v) for k,v in d.items()], sep='\n')

The output is:

a:1
b:2

Note that a more concise version of the previous code snippet is with f-string f'{k}----{v}':

d = {'a': 1, 'b': 2}
print(*[f'{k}----{v}' for k,v in d.items()], sep='\n')

Output:

a----1
b----2

You can learn about the ins and outs of the built-in print() function in the following video:

Method 3: Slicing String Representation

Slicing is a concise way to access a subsequence from an original sequence. You can use slicing on the string representation of a dictionary to access all characters except the first and last ones—that are the curly bracket characters. For example, the expression print(str({'a': 1, 'b': 2})[1:-1]) prints the list as 'a': 1, 'b': 2 without enclosing brackets.

d = {'a': 1, 'b': 2}
print(str(d)[1:-1])
# Output: 'a': 1, 'b': 2

Feel free to dive into slicing next to boost your coding skills:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Programmer Humor

Question: How did the programmer die in the shower? ☠️

Answer: They read the shampoo bottle instructions:
Lather. Rinse. Repeat.

👉 Recommended Tutorial: