0

I have a series of lists with two items in each of them called box1, box2, box3, etc. and am trying to append to a master list to access later. Below is what I thought would work, but it is not. I think it may be something with the string in the append method adding to i, but I am not sure.

This is obviously only adding the string box + i, but I need to figure out a way to loop through and add all similarly named items to this list. What is the best way to accomplish this goal?

box1 = [1,2]
box2 = [3,4]
boxes = []

##Adding strings. I need to add box items instead.
for i in range(11):
    boxes.append("box" + str(i))

I want to see this:

boxes = [[1,2],[3,4]]

FYI: I cannot use imported libraries for this due to the software I am limited to using.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wondergoat77
  • 1,765
  • 9
  • 32
  • 60
  • You shouldn’t have local variables `box1` to `box10` to begin with… Put it in a dictionary, or just directly into `boxes`. – poke Jun 14 '13 at 16:06

7 Answers7

1

You can use eval(), in your case it will be:

boxes = [eval('box%d' %i) for i in range(1,3)]

If you have a list with the box names you want to add to boxes, you can simply do:

boxes = [eval(boxname) for boxname in boxnames]
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
1
for i in range(11):
    box = locals().get('box' + str(i))
    if box is not None:
        boxes.append(box)

Note that it would be better in my opinion to refactor your code so that instead of defining the variables box1, box2, etc. you would create a dictionary and then do something like box_dict['box1'] = [1, 2]. This would allow you to easily lookup these lists later by the name.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 1
    At module level `locals()` is `globals()`, so it would work either way. – mata Jun 14 '13 at 16:16
  • thanks, i think i will explore the dictionary options per this comment. thought i was keeping it simple with a list in a list. I really dont need to resuse the code a lot, but i think enough to justify a dictionary. – wondergoat77 Jun 14 '13 at 16:24
0

This should work:

box1 = [1,2]
box2 = [3,4]
boxes = []
##adding strings, need to add box items instead
for i in range(1,3):
    boxes.append(locals()["box" + str(i)])
mata
  • 67,110
  • 10
  • 163
  • 162
0

This is rather a poor way to code, but you can do it like this

import itertools

boxes=[]
for i in itertools.count(1):
    name='box{}'.format(i)
    if name not in locals():
       break
    boxes.append(locals()[name])

For the no-libraries version:

boxes=[]
i=1
while True:
    name='box{}'.format(i)
    if name not in locals():
       break
    boxes.append(locals()[name])
    i+=1
simonzack
  • 19,729
  • 13
  • 73
  • 118
0

One can use the vars() function to get a dictionary of available variables:

box1 = [1,2]
box2 = [3,4]
boxes = []

##Adding strings. I need to add box items instead
for i in range(11):
    key = "box%d" % i
    if key in vars():
         boxes.append(vars()[key])
boxes

Also, you should consider to check globals().

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
oleg
  • 4,082
  • 16
  • 16
0
 boxDict={}
 boxes=[]
 for i in range(1,11):
      box = [i, i+1]
      boxDict[i] = box
      boxes.append(box)
isaach1000
  • 1,819
  • 1
  • 13
  • 18
0

Your question is similar to Stack Overflow question Convert a string to preexisting variable names, but to save time, you can use getattr(object, name) to get a variable with a particular name out of an object.

You might have something like:

for i in range(11):
    boxes.append(getattr(self, "box"+i))
Community
  • 1
  • 1
monknomo
  • 540
  • 8
  • 26