print dict python code example
Example 1: print key of dictionary python
for key, value in mydic.items() :
print (key, value)
Example 2: printing dictionary in python
mydict = {'score1': 41,'score2': 23}
mydict['score3'] = 45
for i in mydict:
print(i,mydict[i])
Example 3: python print dictionary line by line
for key, value in student_score.items():
print(key, ' : ', value)
Example 4: 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 5: how to write a dict in pytohn
my_dict = {'name': 'Jack', 'age': 26}
print(my_dict['name'])
print(my_dict.get('age'))
Example 6: how to create dictionary in python
d = {'key': 'value'}
print(d)
d['mynewkey'] = 'mynewvalue'
print(d)