How to append a new key value pair to a dictionary in Python

2 Answers

0 votes
dic = {5 : 'python', 8: 'java', 12: 'c', 19: 'c++', 18 : 'c#'}
  
dic.update({'php' : 38}) 

 
print(dic)
  
  
  
'''
run:
  
{5: 'python', 8: 'java', 12: 'c', 19: 'c++', 18: 'c#', 'php': 38}
  
'''

 



answered Jan 29, 2020 by avibootz
0 votes
dict = {
    "name": "Tom",
    "age": 47,
    "langauge": "python", 
    "company":  "microsoft"}
     
     
print(dict)
 
dict.setdefault("state", 'us')

print(dict)
 
   
   
   
'''
run:
   
{'name': 'Tom', 'age': 47, 'langauge': 'python', 'company': 'microsoft'}
{'name': 'Tom', 'age': 47, 'langauge': 'python', 'company': 'microsoft', 'state': 'us'}
  
'''

 



answered Jan 13, 2021 by avibootz

Related questions

1 answer 133 views
2 answers 163 views
2 answers 131 views
1 answer 149 views
2 answers 176 views
...