-1

How to remove a specific element or data from the dictionary? Suppose I have this dictionary

data = {
    'name': 'Instagram',
    'follower_count': 346,
    'description': 'Social media platform',
    'country': 'United States'
}

and I don't want the element 'follower_count': 346 to be printed. What should be the operation?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 4
    Does this answer your question? [Delete an element from a dictionary](https://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary) – Giorgos Xou Jul 27 '21 at 09:43

2 Answers2

0

use dictionary built in pop() method which accept the key name as a parameter and which will remove the specified key value pair off the dictionary.

in your case it will be

data = { 'name': 'Instagram', 'follower_count': 346, 'description': 'Social media platform', 'country': 'United States' }

print(data.pop('follower_count'))
Dave Pradana
  • 145
  • 12
0

Use pop

data = { 'name': 'Instagram', 'follower_count': 346, 'description':'Social media platform', 'country': 'United States' }
data.pop("follower_count")
print(data)

Output:

{'country': 'United States', 'description': 'Social media platform', 'name': 'Instagram'}
Mukul Kumar
  • 564
  • 1
  • 5
  • 24