Fix first element, shuffle the rest of a list/array

Viewed 1981

Is it possible to shuffle only a (continuous) part of a given list (or array in numpy)?

If this is not generally possible, how about the special case where the first element is fixed while the rest of the list/array need to be shuffled? For example, I have a list/array:

to_be_shuffled = [None, 'a', 'b', 'c', 'd', ...]

where the first element should always stay, while the rest are going to be shuffled repeatedly.

One possible way is to shuffle the whole list first, and then check the first element, if it is not the special fixed element (e.g. None), then swap its position with that of the special element (which would then require a lookup).

Is there any better way for doing this?

4 Answers
Related