1

The following code gives me: local variable 'param' referenced before assignment

value_per_label = [(label, value) \ for label, value in zip(gui_names(param),values) \ for (param, values) in parameters] 

What am I doing wrong?

parameters looks like this:

parameters = [("A", (1,3,5)), ("B", (2,3,4))] 

and I wish to convert it to:

value_per_label = [("A_min", 1), ("A_current", 3), ("A_max", 5), ("B_min", 2), ("B_current", 3), ("BA_max", 4)] 
3
  • gui_names[param] if gui_names is a dictionary Commented Feb 6, 2015 at 8:32
  • @Anmol_uppal No its a function returning a tuple of "gui ids". The data being passed to me is in a wierd format so I'm converting it to something more useful. Commented Feb 6, 2015 at 8:33
  • See e.g. docs.python.org/2/tutorial/… Commented Feb 6, 2015 at 8:34

1 Answer 1

3

Let's convert it into a for loop (List comprehensions are basically faster, more readable for loops):

value_per_label = [] for label, value in zip(gui_names(param), values): for (param, values) in parameters: value_per_label.append((label, value)) 

The error is pretty obvious now

Edit: To fix it, change the for loop to this:

 value_per_label = [(value, label) for (param, values) in parameters \ for (label, value) in zip(gui_names(param), values)] 

I think this gives the same result, I didn't test it though.

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

1 Comment

Yes now I understand the issue! Thanks for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.