This returns a list of all the gids. I would like it so that all gids are reachable by their key. e.g
{0: "23172371321","123421412343",1" "12312412214","123124124"}
.
The GID are accessible by the code task_gid_dict[j][i]["gid"]
i = 0
j = 0
task_gid_dict_2 = {}
for i in range(len(task_gid_dict)):
while True:
try:
task_gid.append(task_gid_dict[j][i]["gid"])
i = i 1
except:
j = j 1
break
task_gid_dict_2[j] = task_gid
task_gid_dict_2
At then moment it looks like this task_gid_dict = {0: [{'gid': '1199729685867432', 'name': 'SAMPLE', 'resource_type': 'task', 'resource_subtype': 'default_task'},
...
EXTRA:
task_detail = {}
for k,i in task_gid_dict.items():
task_detail[i] = client.tasks.get_task(task_gid_2[k][i], {'param': 'value', 'param': 'value'}, opt_pretty=True)
task_detail
CodePudding user response:
Your output will look a little different from what you have up top. A dict stores a single value for a single key, but the value can be a list object, so instead of 0: 123456789, 987654321, you'll have 0: [123456789, 987654321].
task_gid_dict_2 = {}
# iterate through key value pairs of a dictionary
for k, v in task_gid_dict.items():
# initialize the key in the new dict with an empty list
task_gid_dict_2[k] = []
# iterate through the sub-dictionaries in each value
for sub_dict in v:
# Add the gid from the sub-dict to your new list
task_gid_dict_2.append(sub_dict['gid'])
That gets you what you want and I think makes the most sense, but if you want to take it a step further, you could do it with a combined dict and list comprehension as well:
task_gid_2 = {k: [sub_d['gid'] for sub_d in v] for k, v in task_gid_dict.items()}