Skip to content
Related Articles

Related Articles

Python – Print dictionary of list values

Improve Article
Save Article
  • Last Updated : 28 Nov, 2021
Improve Article
Save Article

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}

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!