Michael
Michael

Reputation: 822

Python convert a list of tuples to a list of nested dicts

I have a problem that is almost driving me crazy.

I'm programming in python and I have a list of tuples:

my_tuples = [
    ('csrf_token', u'5bc7224ef8f71c8142b4bef1597883b049b83fee'),
    ('fields-team', u'dsada'),
    ('fields-year_from', u'dsa'),
    ('fields-year_to', u'dsa'),
    ('fields-description', u'dsa') ,
    ('fields-team', u''),
    ('fields-year_from', u''),
    ('fields-year_to', u''),
    ('fields-description', u'')
]

I have to transform this list of tuples into this:

 form_data = {
     'csrf_token':u'5bc7224ef8f71c8142b4bef1597883b049b83fee',
     'fields':[
         {
             'team': u'dsada',
             'year_from': u'dsa',
             'year_to': u'dsa',
             'description': u'dsa'
         },
         {
             'team': u'',
             'year_from': u'',
             'year_to': u'',
             'description': u''
         }
     ]
 }

And then I have to delete the second dictionary because every key has an empty value. So the final result will be:

 form_data = {
     'csrf_token':u'5bc7224ef8f71c8142b4bef1597883b049b83fee',
     'fields':[
         {
             'team': u'dsada',
             'year_from': u'dsa',
             'year_to': u'dsa',
             'description': u'dsa'
         }
     ]
 }

Upvotes: 1

Views: 187

Answers (2)

user325787
user325787

Reputation:

my two cents:

import collections

def transform(loft):
    form_data = collections.defaultdict(list)
    internal = {}
    numfield = 0
    for t in my_tuples:
        keys = t[0].split("-")
        if len(keys) == 1:
            form_data[keys[0]] = t[1]
        else:
            internal[keys[1]] = t[1]
            if numfield == 3:
                numfield = -1
                form_data[keys[0]].append(internal)
                internal = {}
            numfield += 1
    return form_data

if __name__ == "__main__":

    my_tuples = [
        ('csrf_token', u'5bc7224ef8f71c8142b4bef1597883b049b83fee'),
        ('fields-team', u'dsada'),
        ('fields-year_from', u'dsa'),
        ('fields-year_to', u'dsa'),
        ('fields-description', u'dsa') ,
        ('fields-team', u''),
        ('fields-year_from', u''),
        ('fields-year_to', u''),
        ('fields-description', u'')
    ]
    trans = transform(my_tuples)
    # cleanup
    trans['fields'] = [f for i, f in enumerate(trans['fields'])
        if trans['fields'][i]['team'] != ''
        and trans['fields'][i]['year_from'] != ''
        and trans['fields'][i]['year_to'] != ''
        and trans['fields'][i]['description'] != '']
    print dict(trans)

Upvotes: 2

ThinkCode
ThinkCode

Reputation: 7961

How about this :

def myfunc(my_tuples):
    mydict = {}
    d = {}
    for kv in my_tuples:
        if kv[0] == 'csrf_token':
            mydict[kv[0]] = kv[1].decode('ascii')
        elif 'fields' in kv[0]:
            d[kv[0]] = kv[1].decode('ascii')
    mydict['fields'] = [d]
    return mydict


def main():
    my_tuples = [
        ('csrf_token', u'5bc7224ef8f71c8142b4bef1597883b049b83fee'),
        ('fields-team', u'dsada'),
        ('fields-year_from', u'dsa'),
        ('fields-year_to', u'dsa'),
        ('fields-description', u'dsa'),
        ('fields-team', u''),
        ('fields-year_from', u''),
        ('fields-year_to', u''),
        ('fields-description', u'')
    ]

    myfunc(my_tuples)


if __name__ == "__main__":
    main()

Upvotes: 0

Related Questions