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}
# 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:


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')



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}
# 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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here