for x in cars:
print (x)
for y in cars[x]:
print (y,':',cars[x][y])
output:
A
color : 2
speed : 70
B
color : 3
speed : 60
Answer from namit on Stack OverflowW3Schools
w3schools.com › python › python_dictionaries.asp
Python Dictionaries
Using the dict() method to make a dictionary: thisdict = dict(name = "John", age = 36, country = "Norway") print(thisdict) Try it Yourself » · There are four collection data types in the Python programming language: List is a collection which is ordered and changeable.
Top answer 1 of 16
187
for x in cars:
print (x)
for y in cars[x]:
print (y,':',cars[x][y])
output:
A
color : 2
speed : 70
B
color : 3
speed : 60
2 of 16
185
You could use the json module for this. The dumps function in this module converts a JSON object into a properly formatted string which you can then print.
import json
cars = {'A':{'speed':70, 'color':2},
'B':{'speed':60, 'color':3}}
print(json.dumps(cars, indent = 4))
The output looks like
{
"A": {
"color": 2,
"speed": 70
},
"B": {
"color": 3,
"speed": 60
}
}
The documentation also specifies a bunch of useful options for this method.
Reddit
reddit.com › r/learnpython › printing a dictionary
r/learnpython on Reddit: Printing a dictionary
August 20, 2021 -
I have this dictionary. It's called odc. It has about a half dozen key:value pairs.
I want to print it out on separate lines - like this
keya valuea
keyb valueb
keyc valuec
And i'm just not quite sure how to do this! Thoughts?
Top answer 1 of 3
3
Use a for loop to pull each key-value pair. You'll need to call the items() method on the dictionary to get both. Here's some code that will probably help: for key, value in odc.items(): print(f"{key} {value}") Assuming the dictionary in the code example above contains { "a": 1, "b": 2, "c": 3 } the above code will print out: a 1 b 2 c 3
2 of 3
2
in adition to the other answers there is: from pprint import pprint pprint( odc ) Which will pretty print your dictionary, it's not exactly what you're looking for, but it's worth knowing about. !docs pprint
GeeksforGeeks
geeksforgeeks.org › python › how-to-print-a-dictionary-in-python
How to Print a Dictionary in Python - GeeksforGeeks
July 23, 2025 - {'name': 'GeeksforGeeks', 'website': 'www.geeksforgeeks.org', 'topics': ['Algorithms', 'Data Structures', 'Python', 'Java']} In this approach, we are using list comprehension to iterate over the key-value pairs of input_dict, and we are printing each pair in key:value format on a separate line. This is similar to For Loop but it is more modern than the traditional one. ... # input dictionary input_dict = { 'name': 'GeeksforGeeks', 'website': 'www.geeksforgeeks.org', 'topics': ['Algorithms', 'Data Structures', 'Python', 'Java'] } # printing dict using list comprehension [print(f"{key}: {value}") for key, value in input_dict.items()]
Python.org
discuss.python.org › python help
Printing the elements of a dictionary that is contained in a list HOW? - Python Help - Discussions on Python.org
February 8, 2022 - Hi, I have tried multiple ways to print this but came up stumped: I want to print the key value pairs in the following dictionary, I can do it with no problems formatting it just as a dictionary, however I am stumped trying to do it from a list. I can print individual entries but can’t get further than that with a loop etc… any help apreciated. users = [{‘index’: 3, ‘name’:‘Dave’, ‘town’:‘Bham’}, {‘index’: 2, ‘name’:‘Alan’, ‘town’:‘Manchester’}] so that the output looks like this: index:...
Coderwall
coderwall.com › p › gmxnqg › pretty-printing-a-python-dictionary
Pretty Printing a Python dictionary (Example)
October 22, 2022 - If you have a dictionary with more than a few items you need to be able to read, the json.dumps() function takes an indent argument that automatically formats things legibly for you: import json ... print json.dumps(my_dict, indent=1)
Google
developers.google.com › google for education › python › python dict and file
Python Dict and File | Python Education | Google for Developers
There's also an items() which returns a list of (key, value) tuples, which is the most efficient way to examine all the key value data in the dictionary. All of these lists can be passed to the sorted() function. ## By default, iterating over a dict iterates over its keys. ## Note that the keys are in a random order. for key in dict: print(key) ## prints a g o ## Exactly the same as above for key in dict.keys(): print(key) ## Get the .keys() list: print(dict.keys()) ## dict_keys(['a', 'o', 'g']) ## Likewise, there's a .values() list of values print(dict.values()) ## dict_values(['alpha', 'omeg
YouTube
youtube.com › amit thinks
How to Print all the Values of a Dictionary in Python - YouTube
In this video, learn how to Print all the Values of a Dictionary in Python. Dictionary represents the key-value pair in Python, enclosed in curly braces. Key...
Published: September 4, 2022
Views: 3K
Mimo
mimo.org › glossary › python › dictionary-dict-function
Python Dictionary: Syntax and Examples [Python Tutorial]
Python · Open in MimoOpen in MimoCopy Code · print(employee_dict.get("email", "Not available")) # Outputs: Not available · You can use an assignment (=) to add or modify a value in a dictionary or add a new key-value pair. To modify an existing value, simply use its associated key.
Codecademy
codecademy.com › learn › dscp-python-fundamentals › modules › dscp-python-dictionaries › cheatsheet
Python Fundamentals: Python Dictionaries Cheatsheet | Codecademy
Print CheatsheetShare · Values in a Python dictionary can be accessed by placing the key within square brackets next to the dictionary. Values can be written by placing key within square brackets next to the dictionary and using the assignment operator (=). If the key already exists, the old value will be overwritten.
Programiz
programiz.com › python-programming › dictionary
Python Dictionary (With Examples)
March 26, 2024 - # change the value of "Italy" key to "Rome" country_capitals["Italy"] = "Rome" print(country_capitals) ... Note: We can also use the update() method to add or change dictionary items. A dictionary is an ordered collection of items (starting from Python 3.7), therefore it maintains the order of its items.
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-print-the-dictionary-in-table-format
Python program to print the dictionary in table format - GeeksforGeeks
September 6, 2024 - print(" NAME ", " AGE ", " COURSE ") # Iterate through the dictionary # to print the data.
Codemia
codemia.io › home › knowledge hub › how to print a dictionary line by line in python?
How to print a dictionary line by line in Python? | Codemia
September 24, 2025 - The most common mistake is printing the whole dictionary object and calling that "line by line." print(data) gives one string representation, not a formatted per-entry output.
GeeksforGeeks
geeksforgeeks.org › python › write-a-dictionary-to-a-file-in-python
Write a dictionary to a file in Python - GeeksforGeeks
July 23, 2025 - Python · from types import resolve_bases import pickle d = {'Name': "bob", 'Age': 28} # writing dictionary to a binary file with open('data.pkl', 'wb') as file: pickle.dump(d, file) # Reading dictionary from the binary file with open('data.pkl', 'rb') as file: res = pickle.load(file) print(res) Output: data.pkl file ·
W3Schools
w3schools.com › python › python_dictionaries_loop.asp
Python - Loop Dictionaries
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well. Print all key names in the dictionary, one by one:
GeeksforGeeks
geeksforgeeks.org › python › python-pretty-print-a-dictionary-with-dictionary-value
Python | Pretty Print a dictionary with dictionary value - GeeksforGeeks
July 12, 2025 - In this example the code defines a nested dictionary named `test_dict` and prints its original structure. The dictionary has keys 'gfg' and 'cs', with associated inner dictionaries containing 'rate' and 'remark' keys. ... # Python3 code to demonstrate working of # Pretty Print a dictionary with dictionary value # Using loops # initializing dictionary test_dict = {'gfg': {'rate': 5, 'remark': 'good'}, 'cs': {'rate': 3}} # printing original dictionary print("The original dictionary is : " + str(test_dict))