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

How can i turn a list into a dict getting something like this?

I have a list like this:

enter image description here

how to get something like this?

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

enter image description here

>Solution :

Start with an empty dict, iterate over the list, and append each value to a list in the dict, creating it if needed.

>>> a_list = [['a', 1], ['b', 18], ['a', 3], ['b', 21], ['a', 51], ['b', 88]]
>>> a_dict = {}
>>> for k, v in a_list:
...     a_dict.setdefault(k, []).append(v)
...
>>> a_dict
{'a': [1, 3, 51], 'b': [18, 21, 88]}
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