[Tutor] sorting lists

Bill Mill bill.mill at gmail.com
Tue Nov 9 07:15:03 CET 2004


On Mon, 8 Nov 2004 23:13:26 -0500, Diana Furr <dleigh0 at carolina.rr.com> wrote:
>  
> I have a couple of lists that are made by user input. They will input one
> thing for the first list and a related thing for the other list. I want the
> list to be sorted alphabetically and then printed. This is an example of the
> problem. 
> After user input 
> list1[oranges, apples] 
> list2[2,4] 
> # The user is saying that they have 2 oranges and 4 apples 
> If I alphabetize the first list the output looks like 2 apples and 4
> oranges. 
> I guess what I'm asking is how do I connect the lists so that when one is
> sorted the other list changes too. I hope that makes sense. 
> Thank you in advance for your help. 
> Diana 

Diana,

I think what you should do is something like this:

In [7]: fruit = ['zucchini', 'oranges', 'apples']

In [8]: fruit_id = [4,2,8]

In [9]: fruit = zip(fruit, fruit_id)

In [10]: fruit
Out[10]: [('zucchini', 4), ('oranges', 2), ('apples', 8)]

In [11]: fruit.sort()

In [12]: fruit
Out[12]: [('apples', 8), ('oranges', 2), ('zucchini', 4)]

In general, it's better to maintain one list of connected elements
than it is to try and maintain two parallel lists of information, for
specifically the reasons you mention.

Peace
Bill Mill
bill.mill at gmail.com


More information about the Tutor mailing list