+ 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?

17th Oct 2018, 10:00 PM
Sir White Cat
Sir White Cat - avatar
3 ответов
+ 6
dict(zip(lst1, random.sample(lst2, k=len(lst2)))) should do the trick
17th Oct 2018, 10:21 PM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
+ 10
Yes, it is possible to take two lists and randomly shuffle them into a single dictionary, in a manner closely resembling your description.
17th Oct 2018, 10:17 PM
Kirk Schafer
Kirk Schafer - avatar
+ 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
19th Oct 2018, 8:01 PM
Devamitra Acharya
Devamitra Acharya - avatar