How to iterate over a dictionary of lists in Python

Here we have a dictionary where the values are lists.

The names of the countries are the keys and the associated values are lists of car brands from the respective countries.

The first for loop goes through the dictionary.

The second for loop goes through each list that we assigned to value_list by unpacking the tuple returned from the items() method of the dictionary.

dict_of_lists = {'germany': ['bmw', 'mercedes'], 'japan': ['honda', 'toyota']}
for key, value_list in dict_of_lists.items():
    print(f'The car brands from {key} are:')
    for item in value_list:
        print(item)
The car brands from germany are:
bmw
mercedes
The car brands from japan are:
honda
toyota

Watch this Content