Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Joel Pendleton
19,230 PointsHow to do I remove brackets and other unnecessary items when printing a dictionary in Python?
I have some code that sorts and prints data from a dictionary:
sorted(sortedScores.values(), reverse=True) print(sortedScores)
This code works, but it prints like this:
How do I remove the brackets, colons and apostrophes from the print?
Thanks.
2 Answers
Ryan Ruscett
23,309 PointsHola,
If you want to print what is in the dictionary without any marker of it being a dictionary. Than don't put it in a dictionary. It's a data structure. Same thing applies to a list and whatever else you use that is a data structure. If you want to use a value inside a dictionary. It wouldn't matter. If you want to print stuff in a dictionary. You can just print the keys and values. If you want the keys and values to be represented in say a properties file. You can use something like this. from ConfigParser import SafeConfigParser. If you just want a plain old string. Than it takes a whole lot more work. I guess what I am asking is why would you want to print a dictionary like a string? Perhaps there is a better data structure. Of course you can print it all without all the stuff, but it's a lot of code for what reason? Thanks!
Jeffrey Ruder
6,408 PointsAnother library I would recommend if you just want a more tidy view of a data structure for whatever reason is "pretty print". The whole point is making pretty views of data structures.
import pprint