You can use a slice to extract the tail of the list, then pass this chunk into shuffle
, then concatenate the parts back together:
import random
playlist = list(range(10))
tail = playlist[1:]
random.shuffle(tail)
playlist = playlist[:1] + tail
print(playlist)
shuffle
is in-place, so it's a bit awkward to use.
Also, if your data structure is requiring a bit of this extra fussing to use cleanly, you might consider refactoring it to a dictionary that uses playlist names as keys and the constituent tracks associated with that playlist as a list of values, each of which is a dict of track data. For example:
playlists = {
"pop": [{"artist": "Rhianna", "track": "Diamonds"}, ...],
"black metal": [{"artist": "Burzum", "track": "Stemmen fra tårnet"}, ...],
"children": [{"artist": "Raffi", "track": "Bananaphone"}, ...]
...
}
Then it's a matter of shuffle(playlists["pop"])
. Obviously, this structure could get complicated pretty quickly depending on how much data you're managing, but all the more reason to set it up cleanly.