Flat a list of containing only one dict

2024/7/5 10:51:21

I have a dict which contain a list product which will contain only one dict:

d = {"thickness": 90.0,"mass_surf": 37.8,"res_therm": 0.75,"codename": "codename1","category": "category1","sub_categories": ["sub_categories1"],"secondary_targets": ["secondary_targets1","secondary_targets2","secondary_targets3"],"product": [{"codename": "codename1","purpose": "purpose1","category": "category1","material": "material1"}]
}

I want to flat the product list of dict to obtain this:

d = {"thickness": 90.0,"mass_surf": 37.8,"res_therm": 0.75,"codename": "codename1","category": "category1","sub_categories": ["sub_categories1"],"secondary_targets": ["secondary_targets1","secondary_targets2","secondary_targets3"],"product.codename": "codename1","product.purpose": "purpose 1","product.purpose": "purpose1","product.category": "category1","product.material": "material1"
}

How can I do this?

product list will always contains only one item

Answer

You can flatten the internal dict and update it back to the original dict. Eg.,

product = d.pop("product")[0]
flattened = {f"product.{key}": value for key, value in product.items()}
d.update(flattened)

This should do the trick.

https://en.xdnf.cn/q/120098.html

Related Q&A

How to breakup a list of list in a given way in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Incrementing a counter while assigning it in python

Is it possible to do the following in python somehow?nodename = node%s % (++num if ct[0] != J else num)Or do I need to first do the increment and the assign it on the next line:if ct[0] != J:num += 1n…

Append two arrays together into one? (Numpy/Python)

I currently have an array of strings and Im trying to join it together with another array of strings to form a complete word to do some web parsing. For example:`Var1 [A B C, .... ....] Var2 …

Python 2.7 Isolate multiple JSON objects in a string

Ive to parse a text file that contains different kind of data. The most challenging is a line that contains three different JSON object (strings) and other data between them. Ive to divide the Json da…

pass different C functions with pointer arrays as the function argument to a class

I am trying to pass different functions which have pointers as arguments to a python function. One example of the input function as input parameter is the given normal function: Sample.pyxfrom cpython …

dynamic filter choice field in django

I am using django-filter to filter results on a page. I am able to use static choice filters like this:filters.pyFILTER_CHOICES = ( (, All States), (AL, Alabama), (AK, Alaska), (AZ, Arizona), #and so f…

How to print a table from a text file and fill empty spaces?

I have the following table in a txt file:|Client | Container weight | Country of Origin | Product code ||S4378 | 450 Ton | China | 457841 || | 350 Ton | Japan…

Open a file in python from 2 directory back

I want to read a file from 2 folders back.. with open(../../test.txt, r) as file:lines = file.readlines()file.close()I want to read from ../../ two folders back. but not work.. How i can do that ?

Do string representations of dictionaries have order in Python 3.4?

I know dictionaries themselves in Python do not have order. However, Im rather curious if when you call str() on a dictionary if it is always in the same order. It appears to be sorted (by key), no mat…

BeautifulSoup Scraping Results not showing

I am playing around with BeautifulSoup to scrape data from websites. So I decided to scrape empireonlines website for 100 greatest movies of all time. Heres the link to the webpage: https://www.empireo…