Python Forum

Full Version: Creating a variable in for loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, this is what I have so far:

for i in range(len(self.my_tuple)):
            "self.furniture{0}".format(i) == self.my_tuple[i]
As you can see this doesn´t work and I cannot find out how to fix it. I want to create a new variable in each for loop but not sure what´s the syntax of it. Thanks.
Why do you want to do this? It's a poor programming idea. What's wrong with using self.my_tuple[3] instead of self.furniture3 ?
Or perhaps you could do self.furniture = self.my_tuple and then use self.furniture[3], which is better than self.furniture3
First of all, you should iterate over the list, not the indexes of the list, as shown here

Second, you shouldn't be turning a list or tuple into separate variables with a number at the end of their name. Keep it in a list to retain the power of the list. self.furniture = list(my_tuple)

Finally, when you do want to assign attributes to an object, use setattr:

for key, value in new_attrs.items():    # looping through a dict
    setattr(self, key, value)
@Gribouillis

I need it for my dict(), there is no way to do it like you have described. Finally it should look like this:

self.dict_crates["self.furniture{0}".format(i)] = self.my_tuple[i]
Can you help me with that?
What do you need help with? That code already works:
>>> class Spam:
...   def __init__(self):
...     self.dict_crates = {}
...     self.my_tuple = (5, 7, 3, 5, 1)
...   def foo(self):
...     for i in range(5):
...       self.dict_crates["self.furniture{0}".format(i)] = self.my_tuple[i]
...
>>> x = Spam()
>>> x.foo()
>>> x.dict_crates
{'self.furniture0': 5, 'self.furniture1': 7, 'self.furniture2': 3, 'self.furniture3': 5, 'self.furniture4': 1}
@nilamo yes this works, but except making an item in a dictionary with specific value (tuple in this case) I also want to create a variable with the same name as a key in dict() is and add an specific image into it. Something like this:

for i in range(len(self.coords_tuple)):
    "self.furniture{0}".format(i) = self.canvas.create_image(self.coords_tuple[i][0], self.coords_tuple[i][1], anchor=NW, image=self.crate_image)
*self.coords_tuple is just a couple like (x,y), not important for this.

And this part: "self.furniture{0}".format(i) doesn´t work here.
Again, furniture0, furniture1, furniture2 is a bad idea. You should keep it as a list.

self.furniture = []
for x, y in self.coords_tuple:   # again, loop through the object, not the indexes
    self.furniture.append(self.canvas.create_image(x, y, anchor = NW, image = self.create_image))
Now you can access them as self.furniture[0], self.furniture[1], self.furniture[2], ...
@ichabod801 ok, this seems clear, but how do I recognize now which key in my dictionary is related to specific item from this list? That´s why did I want to keep name of keys and name of variables the same, to make it simple when working with them.

Example: when working with a key "self.furniture0" I also want to work with self.furniture[0] and so on..
You can put it all in the list:

self.furniture = []
for coords, crate in zip(self.coords_tuple, self.my_tuple):
    data = {'image': self.canvas.create_image(x, y, anchor = NW, image = self.create_image)}
    data['crate'] = crate
    self.furniture.append(data)
The you can access the two parts with self.furniture[2]['image'] and self.furniture[2]['crate']. You could also make a tuple of the two things, and append that. The you would access with self.furniture[2][0] and self.furnitures[2][1].
If the value of a dict is related to the value in a list, then those two values should be kept together, instead of separate.

So instead of a dict of {"furnature0": "canvas"}, and a list of ["furnature"], just have one variable keeping track of both.