If I have a categorical list with only two values, how can I sort so that the values are placed on after another.
Example:
# input list
lst = ['foo', 'bar', 'bar', 'foo', 'bar', 'bar', 'foo', 'foo']
# expected output
['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar']
I have a working solution, but I felt like this could be done smarter.
I also looked into itertools but could not find anything useful for my problem:
my solution:
foo = [val for val in lst if val == 'foo']
bar = [val for val in lst if val == 'bar']
lst2 = [[x, y] for x, y in zip(foo, bar)]
final_list = [val for l in lst2 for val in l]
print(final_list)
['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar']
Note, the lists always have a equal amount of both values