I\'ve such a problem:
There is a list of elements of class CAnswer (no need to describe the class), and I need to shuffle it, but with one constraint -
Another solution:
# memorize position of fixed elements
fixed = [(pos, item) for (pos,item) in enumerate(items) if item.freeze]
# shuffle list
random.shuffle(items)
# swap fixed elements back to their original position
for pos, item in fixed:
index = items.index(item)
items[pos], items[index] = items[index], items[pos]