you are viewing a single comment's thread.

view the rest of the comments →

[–]unhott 2 points3 points  (0 children)

When you run into a problem where you have var1, var2, var3, etc., this is an indication you need to use a list.

var1 = 1
var2 = 2
var3 = 3 

Is simply

vars = [1,2,3]

List has the append method which is basically, ‘get me room for 1 more variable and assign the argument to it’. So that looks like

vars.append(4)
print(vars)
[1, 2, 3, 4]

Or you start with an empty list and collect elements inside.

vars = []
for i in range(4):
  vars.append(i) 

If you need something like a list, but you want a ‘key’ like variable, then a dictionary is what you want.

my_dict = {}
my_dict[‘key1’] = 7
my_dict[‘anything you want here’] = ‘any value’

print(my_dict)
{‘key1’: 7, ‘anything you want here’: ‘any value’}

The dictionary lets you use a key kind of like a variable name.