ch1zra
ch1zra

Reputation: 21

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.

Upvotes: 2

Views: 4858

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336258

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"

Upvotes: 9

Related Questions