w3resource

Python JSON: Access only unique key value of a Python object


9. Access Only Unique Key Values of a Python Object

Write a Python program to access only unique key value of a Python object.

Sample Solution:-

Python Code:

import json
python_obj = '{"a":  1, "a":  2, "a":  3, "a": 4, "b": 1, "b": 2}'
print("Original Python object:")
print(python_obj)
json_obj = json.loads(python_obj)
print("\nUnique Key in a JSON object:")
print(json_obj) 

Output:

Original Python object:
{"a":  1, "a":  2, "a":  3, "a": 4, "b": 1, "b": 2}

Unique Key in a JSON object:
{'a': 4, 'b': 2}
 

Flowchart:

Flowchart: Access only unique key value of a Python object.

For more Practice: Solve these Related Problems:

  • Write a Python program to iterate through a dictionary and output only the keys that have unique values.
  • Write a Python program to filter a dictionary so that each value appears only once, returning a new dictionary with unique values.
  • Write a Python program to use set operations to extract keys from a dictionary whose associated values are unique.
  • Write a Python program to implement a function that returns a dictionary containing only the keys with values that occur exactly once.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to check whether a JSON string contains complex object or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.