+ 5
Is this possible to do in Python?
I want to know if it is possible in Python to create two lists and then make an dictionary that randomly takes one list as values and the other as keys. For example, let's say I want to get Bob and Lily pets. The available pets are Dots and Fluffy. Is it possible to randomly give Bob and Lily pets?
3 Réponses
+ 6
dict(zip(lst1, random.sample(lst2, k=len(lst2)))) should do the trick
+ 10
Yes, it is possible to take two lists and randomly shuffle them into a single dictionary, in a manner closely resembling your description.
+ 2
import random
name_list = list of names
pet_list = list of pets
random.shuffle(pet_list)
dict = dict(zip(name_list, pet_list)
for the sake of simplicity