0

is it possible to have the indexes of two lists linked, and if so, how?

For example:

If I have two lists.

list1 = [100, 132, 87, 3209, 22]
list2 = ["laptop1","laptop2","laptop3","laptop4","laptop5"]

And I were to sort list1 and I also want that list2 will sort the same way, so that i will have this:

list1 = [22, 87, 100, 132, 3209]
list2 = ["laptop5","laptop3","laptop1","laptop2","laptop4"]

How would I do that?

1 Answers1

0

If 22 will always correlate to "laptop5" you should create a dict:

d = dict(zip(list1, list2))

then d is

{100: 'laptop1', 132: 'laptop2', 87: 'laptop3', 3209: 'laptop4', 22: 'laptop5'}

Otherwise, keeping the lists correlated will be tedious and cumbersome.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • Thank you for commenting! But that's not quite what i mean, what i mean is that when i sort list 1, that list 2 will be sorted the same way. So if I use the command "list1.sort", that list2 will also sort as displayed above. Is that possible? – Ferre Molenbeek Nov 10 '20 at 21:09
  • @FerreMolenbeek No, this is not possible. The only option would be to wrap both lists in a class – DeepSpace Nov 11 '20 at 01:13