Suppose I have a ordered list here called T
T = ['foo', 'bar', 'cad']
I then have a set M containing tuples.
M = {
('jack', 'bar'),
('bob', 'foo'),
('let', 'cad')
}
For every item in T, I want to find the corresponding tuple pair in M. So my output would look like the following list
O = [ 'bob', 'jack', 'let' ]
I tried this set comprehension but this of course just iterated the elements as defined in the order of M, I need to define it in the order of T.
answer = [ a for (a,b) in R if b in T ]
As a follow up question, say my M looked like:
M = {
('bar', 'jack'),
('foo', 'bob'),
('cad', 'let')
}
Does this make this easier to solve?
Is it possible to solve this without using a dict? Purely lists, sets and tuples?