how to print the value of a key in python dictionary code example

Example 1: how to print all elements of a dictionary in python

# you can also use items method

my_dict = {"one": 1,"two":2,"three":3,"four":4}

for key,value in my_dict.items():
    print("Key : {} , Value : {}".format(key,value))

Example 2: how to get element from dictionary python

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry

print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']

Example 3: how to print a value from a dictionary in python

dictionary={
  
    "Jeff":{
      	"lastname":"bobson",
        "age":55,
        "working":True
    },
  
    "James":{
      	"lastname":"Bobson",
        "age":34,
        "working":False
    }
}

# For a good format:

for i in dictionary:
    print(i, ":")
    for j in dictionary[i]:
        print("  ", j, ":", dictionary[i][j])
    print()
        

# Output:

Jeff :
   lastname : bobson
   age : 55
   working : True
   
James :
   lastname : Bobson
   age : 34
   working : False
    
# For just a quick reading:

for k, v in dictionary.items():
  print(k, v)
  
# Output: 

Jeff {'lastname': 'bobson', 'age': 55, 'working': True}
James {'lastname': 'Bobson', 'age': 34, 'working': False}