22

How do I use a loop to name variables? For example, if I wanted to have a variable double_1 = 2, double_2 = 4 all the the way to double_12 = 24, how would I write it?

I get the feeling it would be something like this:

for x in range(1, 13):
    double_x = x * 2 
    # I want the x in double_x to count up, e.g double_1, double_2, double_3

Obviously, this doesn't work, but what would be the correct syntax for implementing the looped number into the variable name? I haven't coded for a while, but I do remember there was a way to do this.

2
  • 3
    use a dict. it is exactly why they were created. Commented Nov 28, 2012 at 10:48
  • 1
    Does this answer your question? How do I create variable variables? Commented Aug 4, 2021 at 9:11

5 Answers 5

47

Use a dictionary instead. E.g:

doubles = dict()

for x in range(1, 13):
    doubles[x] = x * 2

Or if you absolutely must do this AND ONLY IF YOU FULLY UNDERSTAND WHAT YOU ARE DOING, you can assign to locals() as to a dictionary:

>>> for x in range(1, 13):
...     locals()['double_{0}'.format(x)] = x * 2
... 
>>> double_3
6

There never, ever should be a reason to do this, though - since you should be using the dictionary instead!

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

3 Comments

+1, but Or if you absolutely must do this he probably doesn't and therefore shouldn't ever.
assigning to locals like this is very dangerous. really should be avoided. you should link some information about it or a more strict warning.
The docs say to avoid modifying it.
7

expanding my comment: "use a dict. it is exactly why they were created"

using defaultdict:

>>> from collections import defaultdict
>>> d = defaultdict(int)

using normal dict:

>>> d = {}

the rest:

>>> for x in range(1, 13):
    d['double_%02d' % x] = x * 2


>>> for key, value in sorted(d.items()):
    print key, value


double_01 2
double_02 4
double_03 6
double_04 8
double_05 10
double_06 12
double_07 14
double_08 16
double_09 18
double_10 20
double_11 22
double_12 24

2 Comments

no specific reason, but he wants ints, i guess you dont need that part really.. but i like them :P
;) I'd rather have an exception if I try to access something that isn't there.
6

Although I doubt you really need to do what you want, here's a way:

namespace = globals()
for x in range(1, 13):
    namespace['double_%d' % x] = x * 2

print double_1
print double_2
   ...
print double_12

globals() returns a dictionary representing the current global symbol table (the dictionary of the current module). As you can see, it's possible to add arbitrary entries to it.

Comments

2

As already mentioned, you should use a dict. Here's a nice easy way to create one that meets your requirements.

>>> {k:k*2 for k in range(1,13)}
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24}

Comments

1

You can use the dict while it don't fit your requirement. But I hope it can help you.

var_dic = {}
for x in range(1, 13):
    var_dic["double_%s"% str(x)] = x * 2
print var_dic

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.