I have a list of lists which represents the options I can chose from.
I have a sequence of indices which represent which option lists I want to take elements from, and in which order.
E.g. if I have
choices = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
sequence = [ 2, 0, 1, 1 ]
I want my output to be
[7, 8, 9, 1, 2, 3, 4, 5, 6, 4, 5, 6]
#index 2 index 0 index 1 index 1
I have found three possible solutions:
choice = sum( ( choices[i] for i in sequence ), [] )
choice = reduce( operator.add, ( choices[i] for i in sequence ) )
choice = [ element for i in sequence for element in choices[i] ]
I would like to know which of these do people find the most pythonic and to know if there are any other elegant solutions.
choice, or would an iterable do? What do you do with it next? \$\endgroup\$