How to attach a list of tuples and dictate in a dict?

How to list a list of tuples and point to a dict?

['f','b','c','d'] (1,2,3) and {'a':'10'}
d excluded for list be compatible with the tuple

output {'f':'1','b':'2','c':'3','a':'10'}
+3
source share
6 answers

You can make dictfrom keys and values ​​as follows:

keys = ['a','b','c','d']
values = (1,2,3)
result = dict(zip(keys, values)) # {'a': 1, 'c': 3, 'b': 2}

Then you can update it with another dict

result.update({ 'f' : 5 })
print result # {'a': 1, 'c': 3, 'b': 2, 'f': 5}
+7
source
dict(zip(a_list, a_tuple)).update(a_dictionary)

when a_list is your list, a_tuple is your tuple, and a_dictionary is your dictionary.

EDIT: If you really wanted to turn the numbers in a tuple into strings, than first:

new_tuple = tuple((str(i) for i in a_tuple))

and pass new_tuple to the zip function.

+3
source

:

dict(zip(['a','b','c','d'], (1,2,3)))

"a", . :

>>> d = {}
>>> d['e'] = 10
>>> d
{'e':10}
+1

, : {'a':'1','a':'10'} .

:

l = ['a','b','c','d']
t = (1,2,3)

d = {}
for key, value in zip(l, t):
    d[key] = value
0

- ?

>>> dict({'a':'10'}.items() + (zip(['f','b','c','d'],('1','2','3'))))
{'a': '10', 'c': '3', 'b': '2', 'f': '1'}
0

Since no one has given an answer that converts the elements of the set to str yet

>>> L=['f','b','c','d']
>>> T=(1,2,3)
>>> D={'a':'10'}
>>> dict(zip(L,map(str,T)),**D)
{'a': '10', 'c': '3', 'b': '2', 'f': '1'}
0
source

Source: https://habr.com/ru/post/1759961/


All Articles