2

List duplicate data concatenation in python

This is in continuation of list issue but here I want to preserve the order of the dict

listData=[('audioVerify', '091;0'), ('imageVerify', 'icon091.gif'), ('bufferVerify', '\x01?')]
    methodList = {}
for item in listData:
    methodList.setdefault(item[0],[]).append(item[1:])
for method in methodList:
    arguments = methodList[method]
    s = [method,arguments]
    print s

when I iterate the list it gives the following

  ['audioVerify', [('091;0',)]]
  ['bufferVerify', [('\x01?',)]]
  ['imageVerify', [('icon091.gif',)]]

but what are the possibilities where I can preserve the order as below:

  ['audioVerify', [('091;0',)]]
  ['imageVerify', [('icon091.gif',)]]
  ['bufferVerify', [('\x01?',)]]
zmarties
  • 4,809
  • 22
  • 39
Ragav
  • 942
  • 4
  • 19
  • 37

1 Answers1

8

I have just what the doctor ordered: OrderedDict

From the examples:

>>> from collections import OrderedDict
>>> # regular unsorted dictionary
>>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}

>>> # dictionary sorted by key
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

>>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

>>> # dictionary sorted by length of the key string
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])

If you have an older versions of python, consult this other SO question.

Community
  • 1
  • 1
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • i am using python2.6 :( is there any other way – Ragav Nov 05 '12 at 03:09
  • 2
    Hmm. I offered this solution because the question was tagged `python-2.7` – Brian Cain Nov 05 '12 at 03:31
  • 1
    As Brian Cain said, you should read this [thread](http://stackoverflow.com/questions/1617078/ordereddict-for-older-versions-of-python). – Nicolas Nov 05 '12 at 06:45
  • 1
    Nice example, but as soon as you want to push new items, you have to recreate the Dict. – user1767754 Apr 08 '15 at 05:57
  • Not sure why the OP thinks this is the best answer because the requested order is not met according to the question. The example shows sorts in many ways but none with index-style; which can be done with `list[i]` and skip the whole `OrderedDict`. Or in the case of origin preservation an item copy to a new list. In the above case the OP needs to add the "value" sorting key prior the use of `ordered`; which is in fact duable for short dict/lists...but a pain if you have thousands..and thousands of entries. – ZF007 Nov 20 '17 at 14:45
  • use `enumerate` should be mentioned with my comment. Enumerate takes work out of hands and places a vlaue of your choice at the start of the tuple. Then ['audioVerify', [('091;0',)]] would become (0, ['audioVerify', [('091;0',)]]), ..etc. See also: [`enumerate`](https://docs.python.org/3/library/functions.html#enumerate) – ZF007 Nov 20 '17 at 16:19
  • @ZF007, please instead add your comment as another answer to the question. – Brian Cain Nov 20 '17 at 16:47
  • @ Brain... Cheers for the advice. I'll do that tomorrow afternoon (including a clear example) and should i remove my comments written here and above? – ZF007 Nov 20 '17 at 16:58
  • @ZF007, sure, you can remove the comments after moving it to an answer. – Brian Cain Nov 20 '17 at 17:41