0

I have a set of variables which are named as such: Var_1 Var_2 Var_3 Var_4 Var_5 . . Var_95

Each of them contain a dictionary. For example:

Var_1 = {
  'Name' = 'Apple',
  'Price' = 1.50
}

I would to create a list without having to manually type all of them into the list by changing the number. I'm kind of lost on how I can do a loop on a variable naming without making them into strings.

list = []
for i in range(1,95): 
  list = list.append["Var_"+i]
edmundgjj
  • 1
  • 1

1 Answers1

0

You can use list comprehension and get all variable values.

[globals()[i] for i in globals() if i.lower().startswith("var")]
bigbounty
  • 16,526
  • 5
  • 37
  • 65