Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

convert list of list to dictionary with some key pattern

I am looking to convert a list of lists which contains some details in the below format into a dictionary.

[['1234567890', 'abcdef', 'xyzd'],
 ['0987654321', 'pqrstuvw', 'oiu'],
 ['6767547879', 'djkaddsd', 'dsad']]

I want it to be in the below format.

{'target0': ['1234567890', 'abcdef', 'xyzd'],
 'target1': ['0987654321', 'pqrstuvw', 'oiu'],
 'target2': ['6767547879', 'djkaddsd', 'dsad']}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’d prefer enumerate in a dictionary comprehension:

>>> {f'target{k}': v for k, v in enumerate(lst)}
{'target0': ['1234567890', 'abcdef', 'xyzd'], 
 'target1': ['0987654321', 'pqrstuvw', 'oiu'], 
 'target2': ['6767547879', 'djkaddsd', 'dsad']}
>>> 

Also I used f-strings.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading