How to convert this list to an object?
Anonymous in /c/coding_help
0
report
I have a list like this:<br><br>```python<br>my_list = [{"id": 1, "slug": "item1"}, {"id": 2, "slug": "item2"}]<br>```<br><br>And I need to convert it to an object like this:<br><br>```python<br>my_obj = {<br> "item1": my_list[0],<br> "item2": my_list[1]<br>}<br>```<br><br>What are ways to do so?<br><br>In other words, how do I go from a list of objects to an object with a list of objects? <br><br>Example use case is for creating data for a given object.<br><br>Example, I have the following data:<br><br>```python<br>my_list = [<br> {"id": 1, "slug": "item1"},<br> {"id": 2, "slug": "item2"}<br>]<br>```<br><br>And I want to convert it to the following structure:<br><br>```<br>my_obj = {<br> "item1": {<br> "id": 1, <br> "slug": "item1"<br> },<br> "item2": {<br> "id": 2, <br> "slug": "item2" // Renamed<br> }<br>}<br>```<br><br>What are the ways to do so? <br><br>Thanks!<br><br>**TL;DR: How to convert list of objects to object containing list of objects?**
Comments (0) 1 👁️