colttt February 2016

create nested dictionarys in python3

I would like to create nested dict in python3, I've the following list(from a sql-query):

 [('madonna', 'Portland', 'Oregon', '0.70', '+5551234', 'music', datetime.date(2016, 9, 8), datetime.date(2016, 9, 1)), ('jackson', 'Laredo', 'Texas', '2.03', '+555345', 'none', datetime.date(2016, 5, 23), datetime.date(2016, 5, 16)), ('bohlen', 'P', 'P', '2.27', '+555987', 'PhD Student', datetime.date(2016, 9, 7))]

I would like to have the following output:

{madonna:{city:Portland, State:Oregon, Index: 0.70, Phone:+5551234, art:music, exp-date:2016, 9, 8, arrival-date:datetime.date(2016, 5, 23)},jackson:{city: Laredo, State:Texas........etc...}}

Can somebody show me an easy to understand code?

I try:

from collections import defaultdict
usercheck = defaultdict(list)
for accname,  div_ort,  standort,  raum,  telefon,  position,  exp, dep in cur.fetchall():
    usercheck(accname).append[..]

but this don't work, I can't think any further myself

Answers


Javier Ayres February 2016

You can use Dict Comprehension (defined here) to dynamically create a dictionary based on the elements of a list:

sql_list = [
    ('madonna', 'Portland', 'Oregon', '0.70', '+5551234', 'music', datetime.date(2016, 9, 8), datetime.date(2016, 9, 1)),
    ('jackson', 'Laredo', 'Texas', '2.03', '+555345', 'none', datetime.date(2016, 5, 23), datetime.date(2016, 5, 16)),
    ('bohlen', 'P', 'P', '2.27', '+555987', 'PhD Student', datetime.date(2016, 9, 7))
]
sql_dict = {
    element[0]: {
        'city': element[1],
        'state': element[2],
        'index': element[3],
        'phone': element[4],
        'art': element[5],
    } for element in sql_list
}

Keep in mind that every item in the dictionary needs to have a key and a value, and in your example you have a few values with no key.


whrrgarbl February 2016

If you have a list of the columns, you can use the zip function:

from collections import defaultdict
import datetime

# list of columns returned from your database query
columns = ["city", "state", "index", "phone", "art", "exp-date", "arrival-date"]
usercheck = defaultdict(list)
for row in cur.fetchall():
    usercheck[row[0]] = defaultdict(list, zip(columns, row[1:]))

print usercheck

This will output a dictionary like:

defaultdict(<type 'list'>, {'madonna': defaultdict(<type 'list'>, {'city': 'Portland', 'art': 'music', 'index': '0.70', 'phone': '+5551234', 'state': 'Oregon', 'arrival-date': datetime.date(2016, 9, 1), 'exp-date': datetime.date(2016, 9, 8)}), 'jackson': defaultdict(<type 'list'>, {'city': 'Laredo', 'art': 'none', 'index': '2.03', 'phone': '+555345', 'state': 'Texas', 'arrival-date': datetime.date(2016, 5, 16), 'exp-date': datetime.date(2016, 5, 23)}), 'bohlen': defaultdict(<type 'list'>, {'city': 'P', 'art': 'PhD Student', 'index': '2.27', 'phone': '+555987', 'state': 'P', 'arrival-date': None, 'exp-date': datetime.date(2016, 9, 7)})})


Jared Goguen February 2016

When using defaultdict, the argument specifies the default value type in the dictionary.

from collections import defaultdict
usercheck = defaultdict(dict)
for accname,  div_ort,  standort,  raum,  telefon, position,  exp, dep in cur.fetchall():
    usercheck[accname]['city'] = div_ort
    usercheck[accname]['state'] = standout
    ...

The keys in the dictionary are referenced using [key], not (key).

Post Status

Asked in February 2016
Viewed 1,186 times
Voted 14
Answered 3 times

Search




Leave an answer