python print value in dictionary code example
Example 1: printing python dictionary values
for k, v in dic.items():
print(k, v)
Example 2: python dict
my_dict = {
'spam': 'eggs',
'foo': 4,
100: 'bar',
2: 0.5
}
print(my_dict['spam'])
print(my_dict['foo'])
print(my_dict[100])
print(my_dict[2])
for key, value in my_dict.items():
print(key, value)
print(len(my_dict))
my_dict['baz'] = 'qux'
my_dict['baz'] = 'quxx'
del my_dict['spam']
print(my_dict.copy())
print(my_dict.fromkeys('added', 100))
print(my_dict.get('foo'))
print(my_dict.items())
print(my_dict.keys())
print(my_dict.values())
my_dict.setdefault('a', 'b')
my_dict.pop('foo')
my_dict.popitem()
my_dict.update({'baz': 'val'})
my_dict.clear()
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 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}