7

I need to submit a formdata which looks like this :

evt: 2001
evt: 1024001
src: mstrWeb.my.fbb.fb.2001
src: mstrWeb.my.fbb.1024001

When I create a python3 dict :

Dict = {'evt': '2001',
    'evt': '1024001',
    'src': 'mstrWeb.my.fbb.fb.2001',
    'src': 'mstrWeb.my.fbb.1024001'}

it removes the duplicate keys by order. I get:

>>> print (Dict)
{'evt': '1024001', 'src': 'mstrWeb.my.fbb.1024001'}

Is there any way I can have the duplicate keys intact in my Dict ?

jpp
  • 159,742
  • 34
  • 281
  • 339
Shekhar Samanta
  • 875
  • 2
  • 12
  • 25
  • Dictionaries can't have duplicate keys. Can't you have a list of all the values associated with the "evt" key? – Carcigenicate Mar 31 '18 at 12:54
  • @Carcigenicate ,its basically a formdata i need to submit using requests.post to a URL . Unfortunately the values can't be in a list – Shekhar Samanta Mar 31 '18 at 12:56
  • Why can't they? You can't have duplicate keys, so you'll need to look for a different way. To get better suggestions, you might need to hive more context to explain why the value can't be a list, since that's the simple work around. – Carcigenicate Mar 31 '18 at 12:57
  • @Carcigenicate, the host URL i am making the POST request does not accept values in a list for same key – Shekhar Samanta Mar 31 '18 at 13:01
  • 1
    But you can just take the values out of the list before submitting the request. If you're submitting it as a JSON, you can't have duplicate keys anyways either. – Carcigenicate Mar 31 '18 at 13:03
  • I found this link , that says instantiating an class object : http://www.wellho.net/mouth/3934_Multiple-identical-keys-in-a-Python-dict-yes-you-can-.html But it returns an object not string. when i add a function to return string , python just removes it – Shekhar Samanta Mar 31 '18 at 13:09
  • They're abusing the hashes returned from objects to add multiple of the same key. They're just wrapping the string in another object, making it so it's simulating identical keys, even though the keys are on fact different. I can't say this is recommended by any means. – Carcigenicate Mar 31 '18 at 13:20
  • This actually worked for my purpose : http://www.wellho.net/resources/ex.php4?item=y107/wp2 – Shekhar Samanta Mar 31 '18 at 13:57
  • @ShekharSamanta, if you have a solution that works for you, please do write it up as an answer so that other users can benefit. – jpp Mar 31 '18 at 17:20

2 Answers2

2

Python dicts have unique keys. There is no getting around that.

One approach might be to make a defaultdict of lists in Python followed by jinga for loops in the form code to iterate the values of the dict.

Alternatively, if you are able to send a json string, this workaround for handling duplicate keys may help:

Given

data = [
    ("evt", "2001"),
    ("evt", "1024001"),
    ("src", "mstrWeb.my.fbb.fb.2001"),
    ("src", "mstrWeb.my.fbb.1024001")
]

Code

class Container(dict):
    """Overload the items method to retain duplicate keys."""

    def __init__(self, items):
        self[""] = ""
        self._items = items

    def items(self):
        return self._items


json.dumps(Container(data))
# '{"evt": "2001", "evt": "1024001", "src": "mstrWeb.my.fbb.fb.2001", "src": "mstrWeb.my.fbb.1024001"}'
pylang
  • 40,867
  • 14
  • 129
  • 121
1

This works for me, I took it from here : Link

class person(object):
        def __init__(self,name):
                self.name = name

        # As it would print for a user
        def __str__(self):
                return self.name

        # As it would print for a debugging person
        def __repr__(self):
                return "'"+self.name+"'"


Dict = {person('evt'): '2001',
        person('evt'): '1024001',
        person('src'): 'mstrWeb.my.fbb.fb.2001',
        person('src'): 'mstrWeb.my.fbb.1024001'}

print (Dict) #outputs

{'evt': '2001', 'evt': '1024001', 'src': 'mstrWeb.my.fbb.fb.2001', 'src': 'mstrWeb.my.fbb.1024001'}
Shekhar Samanta
  • 875
  • 2
  • 12
  • 25