python - Storing multiple get.arguments in a dict -


i receiving html form data , want store 2 of these values dict in 1 key.

i have:

 data={        'name': self.get_argument('name'),        'email': self.get_argument('email'),        'tel': [self.get_arguments('teltype[]'),self.get_arguments('tel[]')],         ......   } 

this gives result of 'tel': [[u'work', u'home'], [u'123456789', u'0000001111223']].

how can store as: {u'work:u'123456789'} instead?

you use zip:

data = {'tel': [[u'work', u'home'], [u'123456789', u'0000001111223']]} data["tel"] = dict(zip(*data["tel"])) {'tel': {u'home': u'0000001111223', u'work': u'123456789'}} 

zip adds corresponding elements each list tuples:

in [18]: data = {'tel': [[u'work', u'home'], [u'123456789', u'0000001111223']]}  in [19]: zip(*data["tel"]) out[19]: [(u'work', u'123456789'), (u'home', u'0000001111223')] 

dict(*zip) creates key value pairs based on tuple contents:

in [20]: dict(zip(*data["tel"])) out[20]: {u'home': u'0000001111223', u'work': u'123456789'} 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -