Swarup Pattnaik
Swarup Pattnaik

Reputation: 11

How to do string substitution for dictionary values

I am trying to the dict's value for key "_string" to be formatted like "tags:6586"

tagid=[6586,6573,6570]

for tid in tagid:
    
    t_id="tags:{}".format(tid)
    
    query={"query": {"_and": [{"_string":"t_id"}]}}
    print (query)

returns

{'query': {'_and': [{'_string': 't_id'}]}}

{'query': {'_and': [{'_string': 't_id'}]}}

{'query': {'_and': [{'_string': 't_id'}]}}

Upvotes: 0

Views: 32

Answers (1)

Victor Paléologue
Victor Paléologue

Reputation: 2342

In query={"query": {"_and": [{"_string":"t_id"}]}} replace "t_id" by str(t_id):

query={"query": {"_and": [{"_string": t_id}]}}

You want to put there the string representation of your variable t_id, not the string "t_id".

Upvotes: 1

Related Questions