8

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 ?

9
  • Dictionaries can't have duplicate keys. Can't you have a list of all the values associated with the "evt" key? Commented Mar 31, 2018 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 Commented Mar 31, 2018 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. Commented Mar 31, 2018 at 12:57
  • @Carcigenicate, the host URL i am making the POST request does not accept values in a list for same key Commented Mar 31, 2018 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. Commented Mar 31, 2018 at 13:03

2 Answers 2

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"}'
Sign up to request clarification or add additional context in comments.

Comments

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'}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.