143

I am struggling with the following problem: I want to convert an OrderedDict like this:

OrderedDict([('method', 'constant'), ('data', '1.225')])

into a regular dict like this:

{'method': 'constant', 'data':1.225}

because I have to store it as string in a database. After the conversion the order is not important anymore, so I can spare the ordered feature anyway.

Thanks for any hint or solutions,

Ben

iruvar
  • 21,786
  • 6
  • 51
  • 81
Ben A.
  • 1,471
  • 2
  • 10
  • 8

8 Answers8

126
>>> from collections import OrderedDict
>>> OrderedDict([('method', 'constant'), ('data', '1.225')])
OrderedDict([('method', 'constant'), ('data', '1.225')])
>>> dict(OrderedDict([('method', 'constant'), ('data', '1.225')]))
{'data': '1.225', 'method': 'constant'}
>>>

However, to store it in a database it'd be much better to convert it to a format such as JSON or Pickle. With Pickle you even preserve the order!

ThiefMaster
  • 298,938
  • 77
  • 579
  • 623
  • 6
    you can [preserve order with `json` too](http://stackoverflow.com/q/6921699/4279). – jfs Nov 23 '13 at 19:59
  • Thanks, and also for the advice using pickle. I would use pickle and I actually do it in other places, but some constraints demand using a dict converted to string. – Ben A. Nov 23 '13 at 20:00
  • Would converting ordereddict to dict loose order? – RamPrasadBismil Feb 06 '16 at 04:06
  • 2
    Yes it does lose order. – nael Jun 01 '16 at 21:21
  • json.loads also in python < 3.0 does not preserve the order. – nael Jun 01 '16 at 22:55
  • 1
    Please note that thos does not work for nested OrderedDictionary data, @thiruvenkadam should be the accepted answer. – Micheal J. Roberts Feb 24 '20 at 13:17
  • @WindUpLordVexxos I disagree, the OP's data wasn't nested, so this answers his question. And going through JSON for this purpose isn't very efficient... – ThiefMaster Feb 24 '20 at 13:20
  • @ThiefMaster Your method works. But for the OPs data. I'd argue that the structure outlined in his question could have been a dummy example, and that we can't assume that anyone reading this question would have his exact same structure. When answering these questions, we must assume a generalistic stance agnostic of the OPs specifics. – Micheal J. Roberts Feb 24 '20 at 13:23
94

Even though this is a year old question, I would like to say that using dict will not help if you have an ordered dict within the ordered dict. The simplest way that could convert those recursive ordered dict will be

import json
from collections import OrderedDict
input_dict = OrderedDict([('method', 'constant'), ('recursive', OrderedDict([('m', 'c')]))])
output_dict = json.loads(json.dumps(input_dict))
print output_dict
John Kugelman
  • 330,190
  • 66
  • 504
  • 555
thiruvenkadam
  • 3,923
  • 2
  • 25
  • 26
  • 8
    Nice move for a script etc., i.e. _I don't care about performance for this one-off task, just gimme dicts so I can pretty print them_ – m_floer Apr 10 '16 at 17:10
8

It is easy to convert your OrderedDict to a regular Dict like this:

dict(OrderedDict([('method', 'constant'), ('data', '1.225')]))

If you have to store it as a string in your database, using JSON is the way to go. That is also quite simple, and you don't even have to worry about converting to a regular dict:

import json
d = OrderedDict([('method', 'constant'), ('data', '1.225')])
dString = json.dumps(d)

Or dump the data directly to a file:

with open('outFile.txt','w') as o:
    json.dump(d, o)
Kyle Neary
  • 829
  • 6
  • 6
6

If you are looking for a recursive version without using the json module:

def ordereddict_to_dict(value):
    for k, v in value.items():
        if isinstance(v, dict):
            value[k] = ordereddict_to_dict(v)
    return dict(value)
spg
  • 8,451
  • 4
  • 33
  • 39
3

Here is what seems simplest and works in python 3.7

from collections import OrderedDict

d = OrderedDict([('method', 'constant'), ('data', '1.225')])
d2 = dict(d)  # Now a normal dict

Now to check this:

>>> type(d2)
<class 'dict'>
>>> isinstance(d2, OrderedDict)
False
>>> isinstance(d2, dict)
True

NOTE: This also works, and gives same result -

>>> {**d}
{'method': 'constant', 'data': '1.225'}
>>> {**d} == d2
True

As well as this -

>>> dict(d)
{'method': 'constant', 'data': '1.225'}
>>> dict(d) == {**d}
True

Cheers

radtek
  • 30,748
  • 10
  • 135
  • 106
1

You can use "dict_constructor" parameters.

xmltodict.parse(text, attr_prefix='',dict_constructor=dict)

greybeard
  • 2,102
  • 7
  • 24
  • 58
CoInz
  • 11
  • 1
-2

Its simple way

>>import json 
>>from collection import OrderedDict

>>json.dumps(dict(OrderedDict([('method', 'constant'), ('data', '1.225')])))
Ramesh K
  • 212
  • 4
  • 13
-2

A version that handles nested dictionaries and iterables but does not use the json module. Nested dictionaries become dict, nested iterables become list, everything else is returned unchanged (including dictionary keys and strings/bytes/bytearrays).

def recursive_to_dict(obj):
    try:
        if hasattr(obj, "split"):    # is string-like
            return obj
        elif hasattr(obj, "items"):  # is dict-like
            return {k: recursive_to_dict(v) for k, v in obj.items()}
        else:                        # is iterable
            return [recursive_to_dict(e) for e in obj]
    except TypeError:                # return everything else
        return obj
E_net4 - Krabbe mit Hüten
  • 24,143
  • 12
  • 85
  • 121
Eponymous
  • 5,133
  • 3
  • 41
  • 42