Python: Print Dictionary Line by Line Example

Let us take a look at how to print a Python Dictionary (dict) line by line.


Let's take an example of a dictionary of countries and its famous city!

locations = {
    'Tuvalu': 'Funafuti',
    'Comoros': 'Moroni',
    'Lesotho': 'Maseru',
    'Liechtenstein': 'Vaduz',
    'San Marino': 'San Marino',
    'Andorra': 'Andorra la Vella',
    'Bhutan': 'Thimphu',
    'Seychelles': 'Victoria',
    'Grenada': 'St. George\'s',
    'Maldives': 'Male',
    'Malta': 'Valletta',
    'São Tomé and Príncipe': 'São Tomé',
    'Vanuatu': 'Port Vila',
    'Antigua and Barbuda': 'St. John\'s',
    'Timor-Leste': 'Dili',
    'Dominica': 'Roseau'

All we need to do is to iterates over the locations dictionary using the items() method which returns key-value pairs from the dictionary.

for country, city in locations.items():
    print(country, "-", city)

We have made use of hyphen (-) as a seperator.

Tuvalu - Funafuti,
Comoros - Moroni,
Lesotho - Maseru,
Liechtenstein - Vaduz,
San Marino - San Marino,
Andorra - Andorra la Vella,
Bhutan - Thimphu,
Seychelles - Victoria,
Grenada - St. George's,
Maldives - Male,
Malta - Valletta,
São Tomé and Príncipe - São Tomé,
Vanuatu - Port Vila,
Antigua and Barbuda - St. John's,
Timor-Leste - Dili,
Dominica - Roseau,

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap