0

I have a number of variables that I would like to create in an automated way based on 2 lists, but unsure how to do so. The objective is unique, but would help me immensely.

list 1 is a list of names -

subqueries = ['name', 'name2', 'name3']

list 2 is generated from a for loop -

def sub_view(qry,subqueries):
   qry_list = []
   for sub in subqueries:
       qry_list.append(qry + """ SELECT * FROM """ +sub+ """ WHERE id='book'""")
   return qry_list

qry_list = sub_view(qry,subqueries)

To get -

subqueries[0] = qry_list[0]
subqueries[1] = qry_list[1]

and so on many times.

2
  • 1
    "I have a number of variables that I would like to create in an automated way" -- why? That is what dictionaries are for. Just use a single variable. Commented Mar 1, 2021 at 15:03
  • 2
    Please use proper indentation in the code. Commented Mar 1, 2021 at 15:09

1 Answer 1

2

You can zip both files and create a dictionary

combined_query= dict(zip(subqueries, qry_list))
Sign up to request clarification or add additional context in comments.

2 Comments

ok cool, and forgive my 'newness' but then does the name dictionary values become something i can extract as a variable? - so if i wanted to extract out the string value for 'name'?
combined_query['name'] should give you the corresponding query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.