players_weight ={'Brian':60,'John':65,'Murphy':68,'Mary':56,'Jane':48}
Using .items to iterate to Print Key and Value
for x, y in players_weight.items():
print(str(x)+' weight is '+ str(y)+' kg')
Brian weight is 60 kg John weight is 65 kg Murphy weight is 68 kg Mary weight is 56 kg Jane weight is 48 kg
Print the Key
for key in players_weight:
print(key)
Brian John Murphy Mary Jane
Print key and value
for key in players_weight:
print(key + ' weight is '+str(players_weight[key]) +' kg')
Brian weight is 60 kg John weight is 65 kg Murphy weight is 68 kg Mary weight is 56 kg Jane weight is 48 kg
Iterate through using keys()
for key in players_weight.keys():
print(key + ' weight is '+str(players_weight[key]) +' kg')
Brian weight is 60 kg John weight is 65 kg Murphy weight is 68 kg Mary weight is 56 kg Jane weight is 48 kg
Getting the Keys only
theKeys =players_weight.keys()
theKeys
dict_keys(['Brian', 'John', 'Murphy', 'Mary', 'Jane'])
Filterting Values , Print Players name where weight is more than or equal to 60
for key in players_weight.keys():
if players_weight[key] >=60:
print (key)
Brian John Murphy
Accessing only Value without Key
for value in players_weight.values():
print(value)
60 65 68 56 48
Modifying Value multiply each Players Weight by 1.2
for key in players_weight.keys():
players_weight[key]= players_weight[key]*1.2
print(key + ' weight is '+str(players_weight[key]) +' kg')
players_weight
Brian weight is 72.0 kg John weight is 78.0 kg Murphy weight is 81.6 kg Mary weight is 67.2 kg Jane weight is 57.599999999999994 kg
{'Brian': 72.0, 'John': 78.0, 'Murphy': 81.6, 'Mary': 67.2, 'Jane': 57.599999999999994}
Swap the Keys and Value
new_player_w={}
for key, value in players_weight.items():
new_player_w[value] = key
new_player_w
{72.0: 'Brian', 78.0: 'John', 81.6: 'Murphy', 67.2: 'Mary', 57.599999999999994: 'Jane'}
Find and delete a Key Value
for key in players_weight.keys():
if key == 'Jane':
del players_weight[key]
players_weight
{'Brian': 72.0, 'John': 78.0, 'Murphy': 81.6, 'Mary': 67.2}