Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (120 points)
How to print dictionaries line by line in python?

1 Answer

0 votes
by (106k points)

Let’s suppose you have a dictionary name vehicle which has two objects such as Vehicle A and vehicle B and both these objects have their own objects such as speed and gear so as per your question the output should be something like as follows:

A

speed: 80

gear: 4

B

speed: 70

gear: 5

So you can use the below-mentioned code to print dictionary line by line where you can use nested for loop and the first for loop will iterate through the dictionary elements and will print the parent objects which is the name of two vehicles and the second for loop will iterate inside those two vehicles and will print their objects line by line.

Vehicle = {'A':{'speed':80'gear':4}, 

           'B':{'speed':70'gear':5}}

for x in Vehicle

      print (x) 

      for y in Vehicle[x]: 

            print (y,':',Vehicle[x][y])

image

Two know more about how a dictionary works you can watch the following video tutorial on Python and, if you want to learn Python with a certificate then you can have a look into the following Python Training Course.

Related questions

0 votes
1 answer
asked Sep 30, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 11, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...