How to remove a key from a dictionary in Python

3 Answers

0 votes
dict = {'python': 5, 'php': 3, 'java': 9, 'c++': 1, 'c':8}

print(dict.pop('php', None))

print(dict)

  
  
  
'''
run:
   
3
{'python': 5, 'java': 9, 'c++': 1, 'c': 8}

'''

 



answered Apr 11, 2021 by avibootz
0 votes
dict = {'python': 5, 'php': 3, 'java': 9, 'c++': 1, 'c':8}

del dict['php']

print(dict)

  
  
  
'''
run:
   
{'python': 5, 'java': 9, 'c++': 1, 'c': 8}

'''

 



answered Apr 11, 2021 by avibootz
0 votes
dict = {'python': 5, 'php': 3, 'java': 9, 'c++': 1, 'c':8}
 
print(dict.pop('javascript', None))
 
print(dict)
 
   
   
   
'''
run:
    
None
{'python': 5, 'php': 3, 'java': 9, 'c++': 1, 'c': 8}
 
'''

 



answered Apr 11, 2021 by avibootz
...