OrderedDict is a class in collection module in python 2.7a3+. Perhaps you can use it from there.<br><br>>>> dir(collections)<br>['Callable', 'Container', 'Counter', 'Hashable', 'ItemsView', 'Iterable', 'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping', 'MutableSequence', 'MutableSet', 'OrderedDict', 'Sequence', 'Set', 'Sized', 'ValuesView', '_Link', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_abcoll', '_chain', '_eq', '_heapq', '_ifilter', '_imap', '_iskeyword', '_itemgetter', '_proxy', '_repeat', '_starmap', '_sys', 'defaultdict', 'deque', 'namedtuple']<br>

<br>~l0nwlf<br><br><div class="gmail_quote">On Mon, Feb 22, 2010 at 10:02 PM, Bryan <span dir="ltr"><<a href="mailto:bryanvick@gmail.com">bryanvick@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

I am looping through a list and creating a regular dictionary.  From<br>
that dict, I create an ordered dict.  I can't think of a way to build<br>
the ordered dict while going through the original loop.  Is there a<br>
way I can avoid creating the first unordered dict just to get the<br>
ordered dict?  Also, I am using pop(k) to retrieve the values from the<br>
unordered dict while building the ordered one because I figure that as<br>
the values are removed from the unordered dict, the lookups will<br>
become faster.  Is there a better idiom that the code below to create<br>
an ordered dict from an unordered list?<br>
<br>
unorderedDict = {}<br>
for thing in unorderedList:<br>
        if <a href="http://thing.id" target="_blank">thing.id</a> in unorderedDict:<br>
                UpdateExistingValue(unorderedDict[<a href="http://thing.id" target="_blank">thing.id</a>])<br>
        else:<br>
                CreateNewValue(unorderedDict[<a href="http://thing.id" target="_blank">thing.id</a>])<br>
<br>
orderedDict = OrderedDict()<br>
for k in sorted(unorderedDict.keys()):<br>
        orderedDict[k]  unorderedDict.pop(k)<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>