简体   繁体   中英

Create a dictionary from one key and list of tuples

I have a list of tuples. For example:

L = [(334, 269, 461, 482), (182, 178, 307, 471),(336, 268, 466, 483), (183, 177, 304, 470)]

The length of the list sometimes changes, it is not constant but the key is the same (for example, u'person')

key = u'person'

I tried to create the dictionary from the key and list, Unfortunately, it took the first tuple only. the output was:

{u'person': (334, 269, 461, 482)}

Here, the value was as tuple but I would like the output will be:

{u'person': [(334, 269, 461, 482), (182, 178, 307, 471),(336, 268, 466, 483), (183, 177, 304, 470)]}

Here, the value is list of tuples.

You can use:

new_dict = {u'person': L}

Output:

{u'person': [(334, 269, 461, 482), (182, 178, 307, 471),(336, 268, 466, 483), (183, 177, 304, 470)]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM