randomizing two lists and maintaining order in python
python
Solution
Use `zip` which has the nice feature to work in 'both' ways.
import random
a = ['Spears', "Adele", "NDubz", "Nicole", "Cristina"]
b = [1,2,3,4,5]
z = zip(a, b)
# => [('Spears', 1), ('Adele', 2), ('NDubz', 3), ('Nicole', 4), ('Cristina', 5)]
random.shuffle(z)
a, b = zip(*z)
Problem
Say I have two simple lists, ``` a = ['Spears', "Adele", "NDubz", "Nicole", "Cristina"] b = [1,2,3,4,5] len(a) == len(b) ``` What I would like to do is randomize `a` and `b` but maintain the order. So, something like: ``` a = ["Adele", 'Spears', "Nicole", "Cristina", "NDubz"] b = [2,1,4,5,3] ``` I am aware that I can shuffle one list using: ``` import random random.shuffle(a) ``` But this just randomizes `a`, whereas, I would like to randomize `a`, and maintain the "randomized order" in list `b`. Would appreciate any guidance on how this can be achieved.