0

I have the following tuple: (1234234, 23, 0)

I would like to make it into a dict of the following form:

{
  app: 1234234,
  sdk: 23,
  done: 0,
}

How can that be achieved?

update: This question is different from this one as it is not implied that it could only be solved by using two tuples.

uber
  • 4,163
  • 5
  • 26
  • 55

4 Answers4

3

Something like the below

values = (1234234, 23, 0)
keys = ['app', 'sdk', 'done']
d = {k: values[idx] for idx, k in enumerate(keys)}
print(d)

output

{'app': 1234234, 'sdk': 23, 'done': 0}
balderman
  • 22,927
  • 7
  • 34
  • 52
2

An alternative approach from @balderman:

values = (1234234, 23, 0)
keys = ['app', 'sdk', 'done']
d = dict(zip(keys, values))
astrochun
  • 1,642
  • 2
  • 7
  • 18
0
t = (1234234, 23, 0)
keys = ["app", "sdk", "done"]
d = {}

for num in t:
    key = keys[t.index(num)]
    d[key] = num

This code will iterate over the numbers in a tuple and assign the keys in order to the numbers within the dictionary.

Daniel
  • 275
  • 1
  • 9
0

a) if you want to chose the keys manually (user input via shell) you can do:

values = (1234234, 23, 0)
yourdict = {input(f"insert key fot value {value}: "): value for value in values}

output:

>>> insert a key for value 1234234: value1
>>> insert a key for value 23: value2
>>> insert a key for value 0: value3

{"value1": 1234234, "value2": 23, "value3": 0}

b) if you want to do it automatically from a list of keys:

values = (1234234, 23, 0)
keys = ["key1", "key2", "key3"]
yourdict = {keys[count]: value for count, value in enumerate(values)}

output:

{"key1": 1234234, "key2": 23, "key3": 0}

c) if you want to assign them numerically:

values = (1234234, 23, 0)
yourdict = {count: value for count, value in enumerate(values)}

output:

{0: 1234234, 1: 23, 2: 0}
Leonardo Scotti
  • 1,069
  • 8
  • 21