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:

responses={
    'greet':'Hello! How can I help you?',
    'types':'Our coffee types are: light roasted, medium roasted, medium dark roasted, dark roasted.',
    'light':'Coffee Bros Paraideli Cup Of Excellence, Lifeboost Coffee, Driftaway Coffee Colombia Antioquia And Burundi Kayanza, Peets Coffee Costa Rica Aurora, Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee.',
    'medium':'Volcanica Coffee Kenya AA, Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Kicking Horse Three Sisters.',
    'dark':'Koa Coffee Estate, Atlas Coffee Club, Lifeboost Coffee Organic Dark Roast, Peets House Blend, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
    'fruit':'Frutis notes coffee: Coffee Bros Paraideli Cup Of Excellence, Peets Coffee Costa Rica Aurora, Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee, Volcanica Coffee Kenya AA, Peets House Blend.',
    'vanilla':'Vanilla notes coffee: Lifeboost Coffee, Driftaway Coffee Colombia Antioquia And Burundi Kayanza.',
    'chocolate':'Chocolate notes coffee: Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Lifeboost Coffee Organic Dark Roast, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
    'timings':'We are open from 9AM to 5PM, Monday to Friday. We are closed on weekends and public holidays.',
    'fallback':'I dont quite understand. Could you repeat that?',
}

So if I pick two different keys like:

'chocolate':'Chocolate notes coffee: Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Lifeboost Coffee Organic Dark Roast, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
'medium':'Volcanica Coffee Kenya AA, Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Kicking Horse Three Sisters.',

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:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("key1",)
parser.add_argument("key2")

command = input() #Input something like "chocolate medium"
args = parser.parse_args(command.split(" "))


#If user has input only one key, print values from that key
if args.key2 == None:
    print(responses[args.key1])
else
    # Then use the find common
    print(find_common_values(responses,args.key1,args.key2))


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

    common = [ k for k in list1 if k in list2]

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.

from itertools import product

responses={
    'greet':'Hello! How can I help you?',
    'types':'Our coffee types are: light roasted, medium roasted, medium dark roasted, dark roasted.',
    'light':'Coffee Bros Paraideli Cup Of Excellence, Lifeboost Coffee, Driftaway Coffee Colombia Antioquia And Burundi Kayanza, Peets Coffee Costa Rica Aurora, Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee.',
    'medium':'Volcanica Coffee Kenya AA, Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Kicking Horse Three Sisters.',
    'dark':'Koa Coffee Estate, Atlas Coffee Club, Lifeboost Coffee Organic Dark Roast, Peets House Blend, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
    'fruit':'Frutis notes coffee: Coffee Bros Paraideli Cup Of Excellence, Peets Coffee Costa Rica Aurora, Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee, Volcanica Coffee Kenya AA, Peets House Blend.',
    'vanilla':'Vanilla notes coffee: Lifeboost Coffee, Driftaway Coffee Colombia Antioquia And Burundi Kayanza.',
    'chocolate':'Chocolate notes coffee: Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Lifeboost Coffee Organic Dark Roast, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
    'timings':'We are open from 9AM to 5PM, Monday to Friday. We are closed on weekends and public holidays.',
    'fallback':'I dont quite understand. Could you repeat that?',
}

def find_common_values(d,key1,key2):
    common = [ k for k in d[key1].split(",") if k in d[key2].split(",")]
    return common

# get all pairs of keys
keys = responses.keys()
pairs = list(product(keys,keys))

for P in pairs: 
    if P[0] != P[1]:
        comm = find_common_values(responses, P[0] , P[1] )
        if len(comm) != 0:
            print( P , comm ) 
    
    
    

Which gives:

('light', 'fruit') [' Peets Coffee Costa Rica Aurora']
('medium', 'chocolate') [' Purity Coffee Flow', ' Out Of The Grey Costa Rica La Minita']
('dark', 'chocolate') [' Lifeboost Coffee Organic Dark Roast', ' Coffee Bros Dark Roast', ' Death Wish Coffee', ' Kicking Horse Coffee Grizzly Claw.']
('fruit', 'light') [' Peets Coffee Costa Rica Aurora']
('chocolate', 'medium') [' Purity Coffee Flow', ' Out Of The Grey Costa Rica La Minita']
('chocolate', 'dark') [' Lifeboost Coffee Organic Dark Roast', ' Coffee Bros Dark Roast', ' Death Wish Coffee', ' Kicking Horse Coffee Grizzly Claw.']
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement