Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a dictionary named as variable

I am using pyodbc to query database and retrieve some data (obviously). What I would like is to create new dictionaries namd according to values returned from query. For example I have a table with 1000 items, and one of collumns in that table is a INT number which is in range 1-51. I want to do a rundown through my table, and create dictionaries named : bought_I, sold_I, created_I, etc... where I stands for INT number. I hope I am clear enough :) I know I could premade those dicts, but range will not always be 1-51, and it's nicer and cleaner to do it programmatically than to hardcode it.

like image 223
ch1zra Avatar asked Mar 12 '26 16:03

ch1zra


1 Answers

Don't.

Create a dictionary bought, and give it keys based on your number:

bought = {}
for number in column_x:
    bought[number] = "whatever object you need here"

Same for sold, created etc.

Or just one big dict:

mydict = {"bought": {}, "sold": {}, "created": {}}
for number in column_x:
    for key in mydict:
        mydict[key][number] = "whatever"
like image 128
Tim Pietzcker Avatar answered Mar 15 '26 05:03

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!