Skip to content
Advertisement

Print the duplicate values from a dictionary in Python

I just wonder if it’s possible to print the duplicate values from a dictionary.

For exemple I have this dictonary:

JavaScript

So if I pick two different keys like:

JavaScript

They have a common coffee name like:

Purity Coffee Flow, Out Of The Grey Costa Rica La Minita

So if I insert the keys for it like: chocolate, medium.

The program need to print only those two duplicates:

Purity Coffee Flow, Out Of The Grey Costa Rica La Minita

It’s possible to print just those 2 words in console which are duplicates in there?

The only thing that I manage to work is to print the duplicates values if values are completly the same, but that’s not my use case.

Advertisement

Answer

EDIT: After some clarification from OP, the keys needs to be input from the console, so I will keep the old answer as well, adding a way to get the keys from user input:

JavaScript

ORIGINAL ANSWER:

This code first calculates all the pairs of keys you have in your dict through itertool.product which is the cartesian product of two lists.

Then uses a function to find the common elements in two lists. The idea is the following

JavaScript

However, instead of list1 and list2 we can use the values in your dict[key]. I noticed that they are strings, so I used the split method to split where commas are.

JavaScript

Which gives:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement