Even though @SuperBiasedMan's answer makes a lot of sense and is the real solution, here's some things you can fix in your current approach.
First of all, do not use the
""" ... """ syntax as comments. They're multiline strings. Instead, prefix each line using
#. (Except when you mean it as a docstring, in which case you may use
""" ... """ syntax).
list1 reperesents customer
list2 reperesents amount of products they have brought
First of all, the variable names do not make a lot of sense.
list2 is a list of
quantities. Better name them as such.
for i in range(0,len(list1)):
Here you're doing a loop at the Python level. Faster and cleaner would be the following:
list3 = zip(list1, list2)
The difference will be that this gives a list of tuples instead of a list of lists. That's hardly going to be a problem in the following.
list3=sorted(list3,key=lambda x: list3[list3.index(x)])
Let's think out loud what the lambda does: first, we find out the position of
list3. Then, we use that position to look up the value at the given position in
list3, (which should, in all reality, just be
x again), and then get the second component.
Better would be to write
list3 = sorted(list3, key=lambda x: x)
Or, even (which we can do because there are no other references to
That's so much shorter!
for i in range(0.len(list3)):
Fixing the typo (
. should be
,) is obvious.
for i in range(0, len(list3)):
Then, let's see if we can write this a bit differently. Yes we can!
list1, list2 = zip(*list3)
At this point,
list2 are tuples. But that we can solve by writing
list1, list2 = map(list, zip(*list3))