I have two lists in python of same length:
listA = [7,6,3,2,1,4,5]
listB = [a,b,c,d,e,f,g]
is their some way (probably easy function) to sort listA and change the values of listB in the same way. Means
listA_new = [1,2,3,4,5,6,7]
and
listB_new = [e,d,c,f,g,b,a]
same question about remove duplicates. E.g. if I have some list
listC = [1,1,4,4,5,6,7]
and
listD = [a,b,c,d,e,f,g]
the result should be:
listC_new = [1,4,5,6,7]
and
listD_New = [a,c,e,f,g]
Try this:
[i for j, i in sorted(zip(listA, listB))]
Output:
listA = [7, 6, 3, 2, 1, 4, 5]
listB = ["a", "b", "c", "d", "e", "f", "g"]
In [5]: [i for j, i in sorted(zip(listA, listB))]
Out[5]: ['e', 'd', 'c', 'f', 'g', 'b', 'a']
for supporting C and D (removing duplicates):
sorted(list({j: i for j, i in reversed(sorted(zip(listC, listD)))}.values()))
.values()
returns ListD:['a', 'c', 'e', 'f', 'g']
and .keys()
returns ListC:[1, 4, 5, 6, 7]