python get values from dictionary code example
Example 1: python dictionary get default
dictionary = {"message": "Hello, World!"}
data = dictionary.get("message", "")
print(data)
Example 2: python get value from dictionary
dict = {'color': 'blue', 'shape': 'square', 'perimeter':20}
dict.get('shape')
dict.get('volume', 'The key was not found')
Example 3: dict get list of values
list(d.values())
Example 4: 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 i in dictionary:
print(i, ":")
for j in dictionary[i]:
print(" ", j, ":", dictionary[i][j])
print()
Jeff :
lastname : bobson
age : 55
working : True
James :
lastname : Bobson
age : 34
working : False
for k, v in dictionary.items():
print(k, v)
Jeff {'lastname': 'bobson', 'age': 55, 'working': True}
James {'lastname': 'Bobson', 'age': 34, 'working': False}
Example 5: how to get value from key dict in python
dictionary = {1:"Bob", 2:"Alice", 3:"Jack"}
entry = dictionary[2]