How to Write a Nested For Loop in One Line Python with File Url and File Name

Question:

 data = json.dumps({'conf':{a:b for a in table_name for b in file_url}})
 print(data)

I have 2 files in file_url.

sales_dy_20221022.csv
sales_wk_20221022

I have 2 table names in table_name

sales_dy
sales_wk

If I write the code like the above I get the result below.

"conf": {
"sales_dy": "sales_dy_20221022.csv",
"sales_wk": "sales_dy_20221022.csv"
}

How can I change the code and get the result below?

"conf": { "sales_dy": "sales_dy_20221022.csv", "sales_wk": "sales_wk_20221022.csv" }
Asked By: Murat

||

Answers:

You are in the right path, use zip.
ex. data = json.dumps({'conf':{a:b for a,b in zip(table_name,file_url)}})

Answered By: spramuditha