I'm looking to create a list whose values alternate based on a set of known patterns. Note that there may not be an equal number of items with each prefix. (2 foos, 3 bars, 3 spams). My current solution is nasty gaggle that .pop()'s from lists built from common prefixes and appends to a new list.
prefix_patterns = ['foo','bar','spam']
inlist = ['fooABC','fooXYZ','barABC','barXYZ','spamABC','bar123','spamXYZ','spam123']
Desired output:
outlist = ['fooABC','barABC','spamABC','fooXYZ','barXYZ','spamXYZ','bar123','spam123']
Current solution (doesn't handle lists of differing lengths):
foos = [value for value in inlist if 'foo' in value]
bars = [value for value in inlist if 'bar' in value]
spams = [value for value in inlist if 'spam' in value]
while foos:
outlist.append(foos.pop())
outlist.append(bars.pop())
outlist.append(spams.pop())
For context: Looking to use this as a sort of throttling mechanism when making requests to 4 different servers.