0

I have this:

listVar=[]
listSel={'one':'x','two':'code'}
for row in listSel:
    exec("%s = '%s'" % (row,listSel[row]))
    listVar.append("%s" %row)
print(one,two)
print(listVar)

I convert the row in a variable with exec(), but i don't know how could i append the new variables in the listVar; next to this i'm looking for inside the variable in a list with listVar.append("%s" %row), my expected answer with print would be:

['x','code']

(Because I'm printing the variables one and two)

But when I print listVar Python answered me:

['one','two']

I don't want to use something like:

listVar.append(one)

Because I'll make a function and I'll not know the rows.

Edit: I need listVar to be [one,two] not ['one','two'].

1 Answers1

1

Edit: Maybe, this is what you needed:

listSel={'one':'x','two':'code'}
one, two = listSel.values()
listVar= [one, two]
print(listVar)
gabriel11
  • 80
  • 6
  • Hello, i don't need the values like a string, i need the values like a variable. I need `listVar` to be `[one,two]` not `['one','two']`. – Victor Nino Vargas Feb 11 '22 at 14:38
  • Sorry for the misunderstanding, could you check this post: [https://stackoverflow.com/questions/11156739/divide-a-dictionary-into-variables](https://stackoverflow.com/questions/11156739/divide-a-dictionary-into-variables) – gabriel11 Feb 11 '22 at 14:53
  • No, that don't work because i will not know the dictionary, the dictionary would the input, for example, if i run `Function({'one':'x','two':'code'})` my list would `[one,two]` but if i input `Function({'qw':'x','as':'code'})` my list would `[qw,as]`. – Victor Nino Vargas Feb 15 '22 at 21:28
  • Why don't you use the dictionary directly? For example, you can access to keys using list1 = list(listSel.keys()), with this code, you have a list of the keys of your dictionary (names) and could access to dictionary values by iterating the list. Something like this [listSel[list1[0]], listSel[list1[1]]] – gabriel11 Feb 17 '22 at 12:35