2

i am struggling with this for loop. I want to update the nested dictionary's value with the list. the following works, but I would like to use for loop to condense the code. "status" is a nested dictionary that has outer key "A" and "B", and binary_list[] just all 1's.

Trucks = ["A", "B"]
Days= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",    
"Saturday", "Sunday"]

requests={"A": {"Monday":0, "Tuesday":0, "Wednesday":0, "Thursday":0     
                "Friday":0, "Saturday":0, "Sunday":0},
          "B": {"Monday":0, "Tuesday":0, "Wednesday":0, "Thursday":0,    
                "Friday":0, "Saturday":0, "Sunday":0}}


binary_list=[1,1,1,1,1,1,1,1,1,1,1,1,1,1]



status["A"]["Monday"]=binary_list[0]
status["A"]["Tuesday"]=binary_list[1]
status["A"]["Wednesday"]=binary_list[2]
status["A"]["Thursday"]=binary_list[3]
status["A"]["Friday"]=binary_list[4]
status["A"]["Saturday"]=binary_list[5]
status["A"]["Sunday"]=binary_list[6]

status["B"]["Monday"]=binary_list[7]
status["B"]["Tuesday"]=binary_list[8]
status["B"]["Wednesday"]=binary_list[9]
status["B"]["Thursday"]=binary_list[10]
status["B"]["Friday"]=binary_list[11]
status["B"]["Saturday"]=binary_list[12]
status["B"]["Sunday"]=binary_list[13]

what I tried:

   for truck in Trucks:
        for day in Days:
            requests[truck][day]=[j for j in binary_list]

this will produce 14 1's in a list as the value of each of the day. I understand why this happens. it's because of the 2 for loops, so I figured I need a nested dictionary comprehenssions to make everything into one line. I am not sure that is correct, but with my limited knowledge that seems to be the right direction. my questions is how do I do that? would someone please help. thanks again for your time.

4 Answers 4

2

You can use an iterator to iterate through the values of binary_list instead:

iter_binary_list = iter(binary_list)
for truck in Trucks:
    for day in Days:
        requests[truck][day] = next(iter_binary_list)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you blhsing, imtinan Azhar, sinwoobang moe A. I was able to read more on iter to learn more about your solution. I really appreciated.
2

You can use dictionary comprehension to do it in a one liner and more "pythonic" way as the following:

it = iter(binary_list)
status = {x: {d: next(it) for d in Days} for x in Trucks}
# outputs: {'A': {'Monday': 1, 'Tuesday': 1, 'Wednesday': 1, 'Thursday': 1, 'Friday': 1, 'Saturday': 1, 'Sunday': 1}, 'B': {'Monday': 1, 'Tuesday': 1, 'Wednesday': 1, 'Thursday': 1, 'Friday': 1, 'Saturday': 1, 'Sunday': 1}}

Comments

1

what you are doing is wrong, j for j in binary_list] will create a list for each requests[truck][day] and this is not what you want, what you want is something like this

binary_list_iterator=0

for truck in Trucks:
    for day in Days:
        requests[truck][day]=binary_list[binary_list_iterator]
        binary_list_iterator+=1

Comments

1

Is this what you want?

requests = {t: {d: 1 for d in Days} for t in Trucks}

Or if you want to iterate everything

requests = {t: {d: binary_list[i*len(Days)+j] for j, d in enumerate(Days)} for i, t in enumerate(Trucks)}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.