Schachjunge Schachjunge - 3 months ago 20
Python Question

Python: Converting a queryset in a list of tuples

i want to turn a queryset into a list of tuples. "result" ist what it should look like:

queryset = [{x:'1',y:'a'}, {x:'2',y:'b'}]
result = [(1,'a'),(2,'b')]

So the solution i do have right now is this one, but it seems to me that there could be a way to shorten the code, and make it more efficient. Is there a way?

My current solution:

result = []
for dic in queryset:


Assuming queryset is supposed to look like this, with 'x' and 'y' as string keys:

>>> queryset = [{'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}]
>>> result = [(q['x'], q['y']) for q in queryset]
>>> result
[('1', 'a'), ('2', 'b')]
>>> # or if x and y are actually the correct names/vars for the keys
... result = [(q[x], q[y]) for q in queryset]