Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating dictionary from the list with each elements as key:value pairs in python [duplicate]

I have a list where the content of list are key:value pairs something like shown below

sample =['ldb:21', 'baseB:4', 'cache-6:55', 'Xauthority:1', 'baseA:4',
         'apmrc:1', 'gz:169', 'dbf:1', 'lst:2', 'sqlite-shm:4', 'ttf:107', 
         'gitignore:6', 'xml:35', 'sbstore:42', 'cache:103', 'jar:1', 
         'desktop:2', 'source:1', 'sqlite3-journal:1', 'TAG:1', '4:1', 
         'usage:6', 'yml:1', 'bin:5', 'docx:9', 'woff:1', 'db:17', 
         'gpg-agent:1', 'V2XO5Y:1', 'dat:2', 'fingerprint:4', 'lz4:1', 
         'cson:2', 'name:1', 'bat:1', 'bau:1', 'converted-launchers:1', 
         'h:14', 'list:2', 'xlb:2', 'dic:1', 'zsh-update:1', 
         'mozlz4:2', 'stderr:8', 'sublime_session:1', 'bak:1', 'dll:42',
         'old:24', 'locale:1', 'cfg:3', 'htm:1', 'odt:1', 'keyring:1', 
         'md~:1', 'pma:2', 'sqlite-journal:1', 'odb:2', 'gradle:3', 'tvc:1', 
         'out:19', 'ico:1', 'icc:1', 'gpg:1', 'dbt:1', '3:1', 'rb:1', 
         'ics:3', 'reg:3', 'metadata:6', 'dirs:1', 'run:1', 'tdb:3', 
         'journal:2', 'zshrc:1', 'little:2', 'pub:1', 'js:245', 
         'asc:1', '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', 'xlc:2', 'jsonlz4:39', 'bashrc:1', 
         'db-journal:3', 'pf2:5', 'localstorage:71', 'isrunning:1', 
         'txt:63', 'orig:1', 'gvdb:1', 'xpi:9', 'php:12', 
         'gitmodules:1', 'log:41', 'swo:1', 'stamp:2', 'vxd:1', 
         'fmt:1', 'py:41']

I want to convert the above list into dictionary key:value pairs. Then I want to convert it into json format.

like image 894
Sjn73 Avatar asked Jan 30 '23 10:01

Sjn73


1 Answers

You can try this:

sample =['ldb:21', 'baseB:4', 'cache-6:55', 'Xauthority:1', 'baseA:4', 'apmrc:1', 'gz:169', 'dbf:1', 'lst:2', 'sqlite-shm:4', 'ttf:107', 'gitignore:6', 'xml:35', 'sbstore:42', 'cache:103', 'jar:1', 'desktop:2', 'source:1', 'sqlite3-journal:1', 'TAG:1', '4:1', 'usage:6', 'yml:1', 'bin:5', 'docx:9', 'woff:1', 'db:17', 'gpg-agent:1', 'V2XO5Y:1', 'dat:2', 'fingerprint:4', 'lz4:1', 'cson:2', 'name:1', 'bat:1', 'bau:1', 'converted-launchers:1', 'h:14', 'list:2', 'xlb:2', 'dic:1', 'zsh-update:1', 'mozlz4:2', 'stderr:8', 'sublime_session:1', 'bak:1', 'dll:42', 'old:24', 'locale:1', 'cfg:3', 'htm:1', 'odt:1', 'keyring:1', 'md~:1', 'pma:2', 'sqlite-journal:1', 'odb:2', 'gradle:3', 'tvc:1', 'out:19', 'ico:1', 'icc:1', 'gpg:1', 'dbt:1', '3:1', 'rb:1', 'ics:3', 'reg:3', 'metadata:6', 'dirs:1', 'run:1', 'tdb:3', 'journal:2', 'zshrc:1', 'little:2', 'pub:1', 'js:245', 'asc:1', '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', 'xlc:2', 'jsonlz4:39', 'bashrc:1', 'db-journal:3', 'pf2:5', 'localstorage:71', 'isrunning:1', 'txt:63', 'orig:1', 'gvdb:1', 'xpi:9', 'php:12', 'gitmodules:1', 'log:41', 'swo:1', 'stamp:2', 'vxd:1', 'fmt:1', 'py:41']
final_data = {a:b for a, b in [i.split(":") for i in sample]}
print(final_data)

Output:

{'ldb': '21', 'baseB': '4', 'cache-6': '55', 'Xauthority': '1', 'baseA': '4', 'apmrc': '1', 'gz': '169', 'dbf': '1', 'lst': '2', 'dll': '42', 'ttf': '107', 'gitignore': '6', 'xml': '35', 'sbstore': '42', 'cache': '103', 'jar': '1', 'desktop': '2', 'source': '1', 'sqlite3-journal': '1', 'TAG': '1', '4': '1', 'usage': '6', 'yml': '1', 'bin': '5', 'docx': '9', 'woff': '1', 'dbt': '1', 'V2XO5Y': '1', 'dat': '2', 'fingerprint': '4', 'lz4': '1', 'name': '1', 'bat': '1', 'bau': '1', 'converted-launchers': '1', 'h': '14', 'list': '2', 'xlb': '2', 'gradle': '3', 'zsh-update': '1', 'stderr': '8', 'sublime_session': '1', 'bak': '1', 'old': '24', 'locale': '1', 'cfg': '3', 'htm': '1', 'odt': '1', 'md~': '1', 'pma': '2', 'sqlite-journal': '1', 'odb': '2', 'dic': '1', 'tvc': '1', 'out': '19', 'ico': '1', 'icc': '1', 'sqlite-shm': '4', '3': '1', 'rb': '1', 'ics': '3', 'py': '41', 'reg': '3', 'metadata': '6', 'dirs': '1', 'run': '1', 'tdb': '3', 'journal': '2', 'zshrc': '1', 'xpi': '9', 'pub': '1', 'js': '245', 'asc': '1', 'xbel': '1', 'properties': '2', 'bash': '1', 'c': '282', 'swo': '1', 'idx': '5', 'lsup7I': '1', 'rdf': '2', 'dmrc': '1', 'Mdg80A': '1', 'pdf': '18', 'xlc': '2', 'jsonlz4': '39', 'bashrc': '1', 'db-journal': '3', 'pf2': '5', 'localstorage': '71', 'isrunning': '1', 'txt': '63', 'orig': '1', 'gvdb': '1', 'little': '2', 'gitmodules': '1', 'log': '41', 'zip': '5', 'stamp': '2', 'vxd': '1', 'fmt': '1', 'gpg': '1', 'gpg-agent': '1', 'cson': '2', 'zcompdump': '1', 'mozlz4': '2', 'db': '17', 'sys': '1', 'php': '12', 'keyring': '1'}
like image 89
Ajax1234 Avatar answered Feb 03 '23 08:02

Ajax1234