Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Looping a dictionary

Hi,

Trying to understand this piece of code. Would appreciate any help
job_args = dict()
if args.job_args:
job_args_tuples = [arg_str.split('=') for arg_str in args.job_args]
job_args = {a[0]: a[1] for a in job_args_tuples}


So in the first line, we create a dictionary of job_args. Can someone please explain line 3 and 4?
by

1 Answer

Bharatv4tg1
temp_list = []
for arg_str in args.job_args:
temp_list.append(arg_str.split("="))
job_args_tuples = temp_list


The fourth line is similar, but it is a dict comprehension instead of a list comprehension.
temp_dict = {}
for a in job_args_tuples:
temp_dict[a[0]] = a[1]
job_args = temp_dict

Login / Signup to Answer the Question.