Sinta555
Sinta555

Reputation: 29

Create a dictionary from SQL query results

I need to create a dictionary from list of tuples. SQL query returns something like this [['a',5], ['b',8], ['c',10]]

Is it possible to use python the dict() constructor ?

Upvotes: 0

Views: 1005

Answers (1)

sebastian
sebastian

Reputation: 9696

Go ahead and try:

dict([['a',5] , ['b',8] , ['c',10]])

on your favourite python console. You'll notice: it works.

The docs tell you why:

dict(iterable, **kwarg)

Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value.

This obviously is under the assumption that a, b and c are what you actually wanted as keys in the dictionary.

Upvotes: 2

Related Questions