0

I need to create variables dynamically based on the data that is coming from my UI.

Sample JSON: Some of the sample JSON I'll get from UI to hit the python code.

data_json = {'key1':'value1','key2':'value2','key3':['abc','def']}
data_json = {'key1':'value2','key2':'value8','key3':['abc','def','ghi','jklmn']}
data_json = {'key1':'value3','key2':'value9','key3':['abc']}
data_json = {'key1':'value4','key2':'value2','key3':['abc','def','xyz']}
data_json = {'key1':'value6','key2':'value2','key3':['abc','def']}

I have data in JSON format in which the length of the "key3" value will keep changing each time. I have to capture those values in separate variables and have to use them later in other functions.

If I pass the first data_json first block of if condition will work and assign it to variables. And if I pass the second data_json second block will define the variables.

Python:

secret = data_json['key1']

if secret in ['value1','value6']: ​
  ​first_value = data_json['key3'][0]
 ​ second_value = data_json['key3'][1]
if secret in ['value2']:
​  first_value = data_json['key3'][0]
​​  second_value = data_json['key3'][1]
  third_value = data_json['key3'][2]
  fourth_value = data_json]'key3'][3]
if secret in ['value3']:
   first_value = data_json['key3'][0]
if secret in ['value4']:
​  first_value = data_json['key3'][0]
​​  second_value = data_json['key3'][1]
  third_value = data_json['key3'][2] 
print("Value in first:%s",first_value)
print("Value in second :%s",second_value)
print("Value in third:%s",third_value)

I'm using conditions to capture those variables.The above code is working fine. But I have to avoid using if conditions. Is there any way to define the variables dynamically on the fly and so that i can use it later in same functions?

3
  • Can you show one of the "other functions" that will use the variables later? Then an answer might better be able to deliver that function what it needs. Commented Jul 24, 2021 at 7:30
  • decrypt_list = {"cred1":first_value,"cred2":second_value} function_call = decrypt(decrypt_list) I have to assign it to a variable and need to pass it to function as parameter Commented Jul 24, 2021 at 8:04
  • I still don't really see what you're trying to do but that looks like you want to place the values into a dict which will be passed to a function. A dict (suggested by @kndofded) lets you have a name for each different value, whereas in a list (suggested by @Moosa Saadat) the items just have positions, not names within the list. Quite possibly your problem could be addressed with some combination of dict and list variables but it's hard to be more specific without seeing where it's going. Commented Jul 24, 2021 at 18:30

2 Answers 2

1

I don't think you are approaching it the right way. For such cases - where we have unknown number of variables - we use lists! Lists in python are of dynamic size. So, you don't need to know the exact size before creating a list.

Therefore, you can store your numbers in a list and then access them using the indices like this:

all_values = data_json['key3']

print("Value in first:%s", all_-values[0])
print("Value in second :%s", all_values[1])
print("Value in third:%s", all_values[2])

Note that here you don't need conditional statements to make sure you are reading the exact number of values (not more or less) from the JSON.

What you are calling dynamic variables are not needed! Wherever you need first_value, you can use all_values[0]. For, second_value, you can use all_values[1] and so on...

Sign up to request clarification or add additional context in comments.

Comments

0

The best way to solve your problem is to save the values in an array and access then via indices, rather than creating separate variables for each element in the array.

data_json = {'key1':'value2','key2':'value8','key3':['abc','def','ghi','jklmn']}
key3_vars = data_json['key3']

for var in key3_vars:
  print(var)

But if you have to create separate variables, then you can use the built-in function exec.

data_json = {'key1':'value2','key2':'value8','key3':['abc','def','ghi','jklmn']}
key3_vars = data_json['key3']

for i, var in enumerate(key3_vars):
  exec(f"key3_var{i} = '{var}'")

print(key3_var0)
print(key3_var1)
print(key3_var2)
print(key3_var3)

Comments

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.