Skip to content
Advertisement

Combining two lists randomly

Hey guys I’m trying to combine these two lists into one dictionary. However, on the second for loop it just keeps going through that and not going back to the first. Any help would be really appreciated. Thank you!

import random
chores = ['dishes','bathroom','vacuum','walk dog']
assign = {}
emails = ['a','b','c','d']

x=0
while x < 4:
    for i in chores:
        randomChore = random.choice(chores)
        chores.remove(randomChore)
    
        for key in emails: 
            assign[key] = randomChore
            x = x + 1

Advertisement

Answer

in your code, the for key in emails: part needed to be removed. it would choose the random chore then loop through, adding the emails without choosing another random chore.

here is the working code.

import random
chores = ['dishes','bathroom','vacuum','walk dog']
assign = {}
emails = ['a','b','c','d']

x = 0
for i in range(0, len(chores)):
    randomChore = random.choice(chores)
    chores.remove(randomChore)

    assign[emails[x]] = randomChore
    x = x + 1
    
print(assign)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement