0

I have defined a variable as below:

time = "0915"

now next I want to create another variable naming it as follows:

x_0915 = "some value" #### idea is to have previously defined variable value in this new variable name itself

how do I make it automatic as I will be having this kind of multiple situations?

so the task is I want to concatenate "x" with the previously defined variable value and use it as a name for the new variable.

1 Answers1

2

Use a dictionary instead of defining dynamic variables and creating a mess.

time = ["0915", "0920", "0925"]
a = {}
for i in time:
    a[f"x{i}"] = "some value"

print(a)

Output

{'x0915': 'some value', 'x0920': 'some value', 'x0925': 'some value'}
Sidharth Mudgil
  • 1,293
  • 8
  • 25