-3

I'm working on a code that generates two lists. say for ex -

L1 = ['a', 'b', 'c']
L2 = [22, 21, 23]

How can i associate both the lists L1 and L2. I need to arrange L1 based on L2

For ex - if L2 is increasing L1 should be -

L1 = ['b', 'a', 'c']

if L2 is decreasing L1 should be -

L2 = ['c', 'a', 'b']

and so on..

hmm
  • 37
  • 8

1 Answers1

3

zip the lists together, sort them, then unzip.

L1 = ['a', 'b', 'c']
L2 = [22, 21, 23]
x = zip(L2, L1)
x.sort()
L1 = zip(*x)[1]
print L1

Result:

('b', 'a', 'c')
Kevin
  • 72,202
  • 12
  • 116
  • 152