Sunday, September 28, 2025
HomeLanguagesPython – Print dictionary of list values

Python – Print dictionary of list values

In this article, we will print a dictionary of list values. Dictionary of list values means a dictionary  contains values as a list of dictionaries

Example:

{‘key1’: [{‘key1′: value,……,’key n’: value}……..{‘key1′: value,……,’key n’: value}}],

————————

————————

‘keyn’: [{‘key1′: value,……,’key n’: value}……..{‘key1′: value,……,’key n’: value}}]}

So we have to get the dictionaries present in the list according to the key. We can get this by using dict.items().

Syntax:

d.items()

we can iterate over the dictionary using for loop

for key,values in data.items():
     for i in values:
          print(key," : ",i)

Example 1: Python code to create a dictionary with student names as key and values as subject details

Python3




# create a dictionary
# with student names as key
# values as subject details
data = {'manoja': [{'subject1': "java", 'marks': 98}, 
                   {'subject2': "PHP", 'marks': 89}],
        'manoj': [{'subject1': "java", 'marks': 78}, 
                  {'subject2': "PHP", 'marks': 79}]}
  
# get the list of data
# using items() method
for key, values in data.items():
    for i in values:
        print(key, " : ", i)


Output:

manoja  :  {'subject1': 'java', 'marks': 98}
manoja  :  {'subject2': 'PHP', 'marks': 89}
manoj  :  {'subject1': 'java', 'marks': 78}
manoj  :  {'subject2': 'PHP', 'marks': 79}

Example 2:

Python3




# create a dictionary
# with student names as key
# values as subject details
data = {'manoja': [{'subject1': "java", 'marks': 98}, 
                   {'subject2': "PHP", 'marks': 89}],
        'manoj': [{'subject1': "java", 'marks': 78},
                  {'subject2': "PHP", 'marks': 79}],
        'ramya': [{'subject1': "html", 'marks': 78}]}
  
# get the list of data
# using items() method
for key, values in data.items():
    for i in values:
        print(key, " : ", i)


Output:

manoja  :  {'subject1': 'java', 'marks': 98}
manoja  :  {'subject2': 'PHP', 'marks': 89}
manoj  :  {'subject1': 'java', 'marks': 78}
manoj  :  {'subject2': 'PHP', 'marks': 79}
ramya  :  {'subject1': 'html', 'marks': 78}
RELATED ARTICLES

Most Popular

Dominic
32324 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6695 POSTS0 COMMENTS
Nicole Veronica
11860 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11918 POSTS0 COMMENTS
Shaida Kate Naidoo
6807 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6771 POSTS0 COMMENTS