user3391890
user3391890

Reputation: 13

For loop and list - syntax

I've got some problem with syntax in python.

Below is a simple example:

Instead that (1):

value = []
value.append(int(self.val1.GetValue()))
value.append(int(self.val2.GetValue()))
value.append(int(self.val3.GetValue()))
value.append(int(self.val4.GetValue()))

I want do sth like that (2):

value = []
for i in range(4):
    value.append(int(self.val + (i + 1) + .GetValue()))

But as you see it's an invalid syntax.

The question is what I must correct in above loop to obtain the same resault like in (1)

Upvotes: 1

Views: 66

Answers (1)

glglgl
glglgl

Reputation: 91149

value = [int(v.GetValue()) for v in (self.val1, self.val2, self.val3, self.val4)]

would probably be the cleanest way.

Even shorter would be to use something with getattr(self, 'val%d' % i).GetValue(), but that's quite ugly.

Upvotes: 2

Related Questions