[Tutor] replacing a loop
Peter Otten
__peter__ at web.de
Mon Jun 24 12:48:28 EDT 2019
johnf wrote:
> Hi folks,
>
>
> I have the following loop (actually repeated many times )
If you have repetetive code look into ways to parameterize it, like
def choices(rows, choices_column, keys_column):
...
>
> def locChoices(self):
> locDS = self.eslocation.getDataSet()
> loc_Choices=['<None>']
> locKeys=[0]
> for row in locDS:
> loc_Choices.append(row['facility'])
> locKeys.append(row['pkid'])
>
> return loc_Choices,locKeys
>
> where locDS is a tuple of dicts and a row is a dict.
>
> Since I use a lot of similar loops to populate many dropdown controls I
> started investigating the use of list comprehensions. But I can't
> figure out how to use them in this loop
You need two loops in this case
choices = ["<None>"] + [row["facility"] for row in ds]
keys = [0] + [row["pkid"] for row in ds]
> and wonder if it will improve
> the performance.
No, list comprehensions are unlikely to improve performance.
> The data is not very big - about a thousand rows -
> give or take.
>
> So what do you guys think?
Are you sure (aka: did you measure that) building these lists takes a
significant amount of time?
If there is a GUI involved any delay you notice is more likely to stem from
filling the widgets than from preparing the lists.
More information about the Tutor
mailing list