0

i try to make an error prevention, in which I look if their is a double definition.

For example:

json1 = {
    "a": "python is good",
    "b": "i like java",
    "c": "python is good"
}

I want to iterate over the values and find out if I got the same value at another key.

To get a message like: "ERROR: double definition"

Is their a good way to compare these?

2

1 Answer 1

1

You can use length(len() function) to compare them.

len(set(json1.values())) == len(json1.values())

You can use this in print like this:

print('ERROR: double definition' if len(set(json1.values())) != len(json1.values()) else 'All good')
Sign up to request clarification or add additional context in comments.

3 Comments

It says, 'list' object has no attribute 'values'.
@Kanat0x Can you tell me what is json1 and how do you get it(From file or not)? And real data of json1(If you can't just give sample data with the same structure)
I took json1 from a file. Thanks for your help, but I solved it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.