Python – mix lists, preserving order

Say you have the following lists:

  • lower_case – A list of all lower-case letters
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  • upper_case – A list of all upper-case letters
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  • digits – A list of digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

and a list of the above, i.e.:

list = [lower_case, upper_case, digits]
 

Now, you want to make a single list with the following properties:

  • All elements of the given lists need to be present
  • The order of elements of each of the individual lists needs to be preserved
  • The final list needs to be randomized

In our case, the final list would be a list of all lower-case letters, upper-case letters and digits, where all individual categories are still ordered (e.g. ‘a’ needs to come before ‘b’ in the final list), but randomized. Here’s one way to do it:

  • Start from the starting list
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
  • Zip the lists to get the ids (which are conveniently list indices in the starting list) [
(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], 0),
(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'], 1),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 2)]
  • For each list, create the list which has the same length, but whose every element is the index of that list. Make a master index list that contains these
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]]
  • Flatten the master index list
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
  • Shuffle the master index list
[2, 2, 1, 0, 1, 1, 1, 0, 2, 0, 1, 1, 1,
2, 1, 0, 0, 0, 1, 0, 1, 2, 1, 0, 1, 0,
0, 1, 1, 1, 2, 0, 0, 0, 1, 0, 2, 1, 1,
1, 2, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0,
0, 0, 2, 0, 1, 0, 0, 0, 2, 0]
  • Create iterators for each of the starting lists. Put them in the iterators list
  • Go through the master index list and pick the iterator from the iterators list based on the index which is given by the current element
[0, 1, 'A', 'a', 'B', 'C', 'D', 'b', 2, 'c',
'E', 'F', 'G', 3, 'H', 'd', 'e', 'f', 'I',
'g', 'J', 4, 'K', 'h', 'L', 'i', 'j', 'M',
'N', 'O', 5, 'k', 'l', 'm', 'P', 'n', 6,
'Q', 'R', 'S', 7, 'T', 'o', 'p', 'U', 'q',
'r', 'V', 'W', 'X', 'Y', 's', 't', 'u', 8,
'v', 'Z', 'w', 'x', 'y', 9, 'z']

In something readable :), i.e. Python:

#!/usr/bin/python
from __future__ import print_function
import random
import itertools
import string

def mix_lists(lists):
  id_pairs = zip(lists, range(len(lists))) 
  print(id_pairs)
  id_lists = [[id] * len(list) for (list, id) in id_pairs]
  print(id_lists)
  id_list = [id for sub_list in id_lists for id in sub_list]
  print(id_list)
  random.shuffle(id_list)
  print(id_list)
  iterators = [iter(list) for list in lists]
  mixed_lists = [iterators[id].next() for id in id_list]
  return mixed_lists

lower_case = 
upper_case = 
digits = range(10)
lists = [lower_case, upper_case, digits]

print(lists)

for i in range(1):
  print(mix_lists(lists))