0

In my scinario, I am having data as kind of a sorted list:

  sample =  ['1:4', '2:2', '3:1', '4:1', '5:1', 'B5S94Y:1', 'DB:4', 'ICEauthority:1', 'JPG:1', 'MZPZ5Y:1', 'Mdg80A:1', 'TAG:1', 'V2XO5Y:1', 'Xauthority:1', 'apmrc:1', 'asc:1', 'bak:1', 'baseA:4', 'baseB:4', 'bash:1', 'bash_history:1', 'bash_logout:1', 'bashrc:1', 'bat:1', 'bau:1', 'bdic:1', 'bin:5', 'c:282', 'cache-6:55', 'cache:103', 'cfg:3', 'conf:15', 'converted-launchers:1', 'cson:2', 'css:34', 'dat:2', 'db-journal:3', 'db-shm:1', 'db-wal:1', 'db:17', 'dbf:1', 'dbt:1', 'deb:3', 'desktop:2', 'dic:1', 'dirs:1', 'dll:42', 'dmrc:1', 'docx:9', 'exc:1', 'exe:20', 'final:1', 'fingerprint:4', 'fmt:1', 'gif:5', 'gitconfig:1', 'gitignore:6', 'gitkeep:2', 'gitmodules:1', 'gpg-agent:1', 'gpg:1', 'gradle:3', 'gvdb:1', 'gz:169', 'h:14', 'htm:1', 'html:43', 'icc:1', 'ico:1', 'ics:3', 'idx:5', 'info:1', 'ini:14', 'ino:1', 'isrunning:1', 'jar:1', 'java:7', 'journal:2', 'jpg:16', 'js-20170612122310:1', 'js-20170816210634:1', 'js:245', 'json:367', 'jsonlz4:39', 'kbx:1', 'keyring:1', 'keystore:1', 'ldb:23', 'list:2', 'little:2', 'locale:1', 'localstorage-journal:71', 'localstorage:71', 'lock:1', 'log:41', 'lst:2', 'lsup7I:1', 'lz4:1', 'm:1', 'map:2', 'md:92', 'md~:1', 'metadata-v2:6', 'metadata:6', 'mozlz4:2', 'name:1', 'nls:1', 'odb:2', 'odt:1', 'old:24', 'orig:1', 'otf:31', 'out:19', 'pack:5', 'pak:1', 'parentlock:2', 'pb:32', 'pdf:18', 'pf2:5', 'php:12', 'pl:1', 'pma:2', 'png:2348', 'pro:1', 'profile:1', 'properties:2', 'pset:42', 'pub:1', 'py:41', 'pyc:4', 'rb:1', 'rcache:2', 'rdf:2', 'reg:3', 'run:1', 'sample:72', 'sbstore:42', 'sdv:1', 'sh:27', 'so:2', 'source:1', 'spec:9', 'sqlite-journal:1', 'sqlite-shm:4', 'sqlite-wal:4', 'sqlite3-journal:1', 'sqlite3:1', 'sqlite:24', 'stamp:2', 'stderr:8', 'stdout:8', 'sth:19', 'sublime_session:1', 'sudo_as_admin_successful:1', 'svg:124', 'swo:1', 'swp:2', 'sys:1', 'tdb:3', 'thm:1', 'trashinfo:3', 'tsv:1', 'ttf:107', 'tvc:1', 'txt:63', 'update-timestamp:1', 'usage:6', 'viminfo:1', 'vxd:1', 'woff:1', 'x86_64-pc-linux-gnu:1', 'xba:1', 'xbel:1', 'xcu:1', 'xinputrc:1', 'xlb:2', 'xlc:2', 'xml:35', 'xpi:9', 'xsession-errors:1', 'yml:1', 'zcompdump:1', 'zip:5', 'zsh-template:1', 'zsh-theme:143', 'zsh-update:1', 'zsh:235', 'zsh_history:1', 'zshrc:1']

So after converting this above list to a dictionary with the following code:

dict_val = {a:b for a, b in [i.split(":") for i in sample]}

The output will be:

{'baseA': '4', 'baseB': '4', 'cache-6': '55', 'Xauthority': '1', 'gitmodules': '1', 'apmrc': '1', 'gz': '169', 'dbf': '1', 'kbx': '1', 'sqlite-shm': '4', 'dbt': '1', 'gitignore': '6', 'xml': '35', 'sbstore': '42', 'cache': '103', 'jar': '1', 'desktop': '2', 'source': '1', 'sqlite3-journal': '1', 'TAG': '1', '4': '1', 'tsv': '1', 'spec': '9', 'bin': '5', 'docx': '9', 'woff': '1', 'db': '17', 'V2XO5Y': '1', 'dat': '2', 'fingerprint': '4', 'lz4': '1', 'name': '1', 'bat': '1', 'bau': '1', 'converted-launchers': '1', 'h': '14', 'list': '2', 'lst': '2', 'gradle': '3', 'zsh-update': '1', 'mozlz4': '2', 'stderr': '8', 'sublime_session': '1', 'bak': '1', 'isrunning': '1', 'locale': '1', 'cfg': '3', 'htm': '1', 'odt': '1', 'xlb': '2', 'md~': '1', 'pma': '2', 'sqlite-journal': '1', 'odb': '2', 'dic': '1', 'tvc': '1', 'out': '19', 'ico': '1', 'icc': '1', 'dll': '42', '3': '1', 'rb': '1', 'ics': '3', 'py': '41', 'journal': '2', 'metadata': '6', 'dirs': '1', 'run': '1', 'tdb': '3', 'DB': '4', 'zshrc': '1', 'xpi': '9', 'pub': '1', 'js': '245', 'asc': '1', 'ldb': '23', 'xlc': '2', 'xbel': '1', 'properties': '2', 'bash': '1', 'sys': '1', 'c': '282', 'zip': '5', 'idx': '5', 'lsup7I': '1', 'zcompdump': '1', 'rdf': '2', 'dmrc': '1', 'Mdg80A': '1', 'pdf': '18', 'reg': '3', 'jsonlz4': '39', 'bashrc': '1', 'db-journal': '3', 'pf2': '5', 'localstorage': '71', 'old': '24', 'txt': '63', 'orig': '1', 'gvdb': '1', 'little': '2', 'pyc': '4', 'java': '7', 'log': '41', 'swo': '1', 'stamp': '2', 'vxd': '1', 'fmt': '1', 'gpg': '1', 'zsh-template': '1', 'pb': '32', 'gif': '5', 'json': '367', '2': '2', 'js-20170612122310': '1', 'swp': '2', 'bash_logout': '1', 'final': '1', 'pl': '1', 'gpg-agent': '1', 'sdv': '1', 'x86_64-pc-linux-gnu': '1', 'parentlock': '2', 'cson': '2', 'rcache': '2', 'otf': '31', 'usage': '6', 'bash_history': '1', 'localstorage-journal': '71', 'update-timestamp': '1', 'png': '2348', 'exc': '1', 'info': '1', 'md': '92', 'js-20170816210634': '1', 'sth': '19', 'yml': '1', 'sqlite-wal': '4', 'deb': '3', 'zsh': '235', 'pack': '5', 'zsh_history': '1', 'sqlite': '24', 'stdout': '8', 'lock': '1', 'pro': '1', 'gitkeep': '2', 'jpg': '16', 'sample': '72', 'ino': '1', 'pset': '42', 'ini': '14', 'conf': '15', 'xcu': '1', 'sudo_as_admin_successful': '1', 'xsession-errors': '1', 'keystore': '1', 'nls': '1', 'sh': '27', 'bdic': '1', '1': '4', 'html': '43', '5': '1', 'MZPZ5Y': '1', 'sqlite3': '1', 'pak': '1', 'ttf': '107', 'css': '34', 'profile': '1', 'map': '2', 'metadata-v2': '6', 'm': '1', 'zsh-theme': '143', 'trashinfo': '3', 'ICEauthority': '1', 'php': '12', 'B5S94Y': '1', 'viminfo': '1', 'exe': '20', 'db-shm': '1', 'xinputrc': '1', 'svg': '124', 'keyring': '1', 'JPG': '1', 'thm': '1', 'gitconfig': '1', 'so': '2', 'xba': '1', 'db-wal': '1'}

So I want to know why the order of the output data has changed compared to the input data and how to get the output in the same sorted order as the input is given!!

Sjn73
  • 189
  • 2
  • 8
  • Python dictionaries are (currently) unordered. In future versions, this may change (but remain a detail of implementation rather than a part of the API): see [this](https://stackoverflow.com/questions/39980323/dictionaries-are-ordered-in-python-3-6) for example. – SwiftsNamesake Oct 07 '17 at 01:40
  • If you want to preserve the order, you should probably use an `OrderedDict`. – SwiftsNamesake Oct 07 '17 at 01:41
  • Use an OrderedDict https://docs.python.org/2/library/collections.html#collections.OrderedDict – mechanical_meat Oct 07 '17 at 01:42
  • @SwiftsNamesake Why the elements are not arranged in the order the inputs are given??? – Sjn73 Oct 07 '17 at 01:50

1 Answers1

3

dicts in python versions older than 3.6 do not have any notion of ordering. If you want a dictionary that maintains order, you'll want the collections.OrderedDict data structure.

from collections import OrderedDict    
mapping = OrderedDict(i.split(":") for i in sample)

mapping is an OrderedDict, a subclass of dict which supports all the basic dict functionality, plus the ordering.

Also, do not use dict as a variable name, it shadows the builtin class dict.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • @COLDSPEED : But while adding elements into dictionary why they got reagganged?? – Sjn73 Oct 07 '17 at 01:52
  • 1
    @Sjn73 Read: `dicts in python versions older than 3.6 do not have any notion of ordering.` It's also mentioned in the documentation, if you read it. – cs95 Oct 07 '17 at 02:17