-2

I wrote a below python script to print all the values associated with keys in a python dictionary. I created python dictionary using values from rest api calls I made. The dictionary has duplicate keys.

dict = {'a':'b', 'a':'c', 'b':'d'}.

I have been through post Is there a way to preserve duplicate keys in python dictionary.

I am able to get the desired output using below script

import collections

data = collections.defaultdict(list)

val=input("Enter a vlaue:")

for k, v in (('a', 'b'), ('a', 'c'), ('b', 'c')):

     data[k].append(v)

#print(list(data.keys()))

if str(val) in list(data.keys()):

    for i in data[val]:

        print(i)

I am struck at converting dictionary to tuple of tuples. eg: {'a':'b', 'a':'c', 'b':'d'} to (('a', 'b'), ('a', 'c'), ('b', 'c')). Is there is a way to do this without changing the duplication value (I need duplicate keys)?

jpp
  • 159,742
  • 34
  • 281
  • 339
sudhir
  • 219
  • 5
  • 17

3 Answers3

0

your dict does not have duplicate keys, that is not possible in python without monkey patching

what you have created is a dictionary of lists that contain the values {a:[b,c],b:[d]}

to convert such a dict into tuples, simply iterate over the lists

data = {"a":["b","c"], "b":["d"]}

def to_tuples(data_dictionary):
    for key, values in data_dictionary.items():
        for value in values:
            yield key, value

print(list(to_tuples(data)))
>>>[('a', 'b'), ('a', 'c'), ('b', 'g')]
Derte Trdelnik
  • 2,656
  • 1
  • 22
  • 26
  • My ask is to convert dictionary {'a':'b', 'a':'c', 'b':'d'} to (('a', 'b'), ('a', 'c'), ('b', 'c')). or please let me know how to convert {'a':'b', 'a':'c', 'b':'d'} to {"a":["b","c"], "b":["d"]}. – sudhir Jan 24 '18 at 02:28
  • when you write {'a':'b', 'a':'c', 'b':'d'} in python, it discards the 'a':'b' part, duplicate keys do not exist in python, the code you have written in your question creates {"a":["b","c"], "b":["d"]} and you asked to convert it to tuples... – Derte Trdelnik Jan 24 '18 at 11:39
0

Maybe this what you want:

>>> import collections
>>> 
>>> data = collections.defaultdict(list)
>>> 
>>> for k, v in (('a', 'b'), ('a', 'c'), ('b', 'c')):
...     data[k].append(v)
... 
>>> print(dict(data))
{'a': ['b', 'c'], 'b': ['c']}
>>> 
>>> l = []
>>> for k, v in data.items():
...     l.extend((k, v2) for v2 in v)
... 
>>> print(tuple(l))
(('a', 'b'), ('a', 'c'), ('b', 'c'))

Hope it helps. =)

bla
  • 1,840
  • 1
  • 13
  • 17
  • Nope. Doesn't help. My ask is to convert dictionary {'a':'b', 'a':'c', 'b':'d'} to (('a', 'b'), ('a', 'c'), ('b', 'c')) – sudhir Jan 24 '18 at 02:34
  • Well, I am not sure how to proceed. How would you relate `b` and `c`? In your sample code you provide a dict in the form of `{'a': ['b', 'c'], 'b': ['c']}` which clearly makes `b` related to `c` but the one you provide in the question is different. – bla Jan 24 '18 at 03:13
0

Dictionaries have unique keys. This does what I think you want:

data = {'a': ['b', 'c'], 'b': ['c']}

data_tuples = tuple((k, i) for k, v in data.items() for i in v)

# output: (('a', 'b'), ('a', 'c'), ('b', 'c'))
jpp
  • 159,742
  • 34
  • 281
  • 339
  • My ask is to convert dictionary {'a':'b', 'a':'c', 'b':'d'} to (('a', 'b'), ('a', 'c'), ('b', 'c')). or please let me know how to convert {'a':'b', 'a':'c', 'b':'d'} to {"a":["b","c"], "b":["d"]} – sudhir Jan 24 '18 at 02:52
  • 1
    My answer is {'a':'b', 'a':'c', 'b':'d'} is not a dictionary. A dictionary may not have duplicate keys. – jpp Jan 24 '18 at 02:53