0

one of my dictionary contains multiple values for a single key. I have worked on simple dictionary with single value. I am new to python not able to figure out how to handle this. I need to update a database table where "field" will be the column and the values will be from "rows" as in the below sample.

"field" key will be the column and "rows" key will be values in database table for corresponding columns.

dictn = {
    u'field': [u'time',
               u'met1',
               u'met2',
               u'met3',
               u'met4',
               u'met5',
               u'met6',
               u'met7'],
    u'set': 0,
    u'messages': [{u'text': u'string',
                   u'type': u'INFO'}],
    u'rows': [[u'2016-01-00:00',
               None,
               u'0.24',
               None,
               u'0.24',
               None,
               u'0.16',
               u'60'],
              [u'2016-01-00:01:00:00',
               None,
               u'0.00',
               None,
               u'0.00',
               None,
               u'0.003500',
               u'60'],
              [u'2016-01-00:02:00.00',
               None,
               u'0.64',
               None,
               u'0.64',
               None,
               u'0.26',
               u'60']]
}
Deb
  • 193
  • 1
  • 3
  • 20

2 Answers2

0

You can iterate over list of lists of values i.e. dictn['rows'] and use every list of values along with list of column names i.e. dictn['field'] to save all the records to database.

for value_lst in dictn['rows']:
  record_dict = dict(zip(dictn['field'], value_lst))
  db_model.save(record_dict)

Where db_model.save() takes a dictionary of single database record in the format

{
  u'time': u'2016-01-00:00',
  u'met1': None,
  ... so on for all the fields ...
}

and saves it in the database.

Muhammad Tahir
  • 5,006
  • 1
  • 19
  • 36
  • this will not iterate for all the values,, i see it iterates only once with column name and value – Deb Jan 07 '16 at 15:14
  • First `for value_lst in dictn['rows']` will iterate for all the sets of values not just one. Second `dict(zip(dictn['field'], value_lst))` takes list of column names i.e. `dictn['field']` and (single record) list of values i.e. `value_lst` and returns a dictionary of the format I mentioned later in my answer. And then in each iteration the `record_dict` will be new for each record in dictn['rows']` and will be passed to a method i.e. `db_model.save(record_dict)` which will save it to database. – Muhammad Tahir Jan 07 '16 at 15:20
0

a single key in a dictionary can refer to only a single value

In the example you gave the key u'field' contains a single value which is a list of elements i.e

[u'time',u'met1',u'met2',u'met3',u'met4',u'met5',u'met6',u'met7']

hence in order to access the list stored at key value u'field you should use

list_of_elements = dictn[u'field']

there after you can iterate across all the elements in the list using a for loop

for element in list_of_elements:
    # do something

Hope this link can help you

append multiple values for one key in Python dictionary

Community
  • 1
  • 1
Namit Juneja
  • 498
  • 5
  • 13