I've written the code below:
items = [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]]
sorted_items = sorted(items, key=lambda x: x[0])
for i in sorted_items:
if i[0] == (i+1)[0]:
The above code gives me an error- particularly for the last line of code. My question is, how do I reference the first index of lists within lists? I hope the question is clear from the last line of my code.
i.e. i[0] is okay, but Python doesn't allow (i+1)[0]; how else can I write this?
Thanks.
Just a follow on from my question above:
for i in range(len(sorted_items)-1):
if sorted_items[i][0] == sorted_items[i+1][0]:
final_sorted_items = sorted(sorted_items, key=lambda x:x[1], reverse=True)
print(final_sorted_items)
So I have added the line but it doesn't do the job that I am after. I basically want to sort the smaller lists in descending order if the first index is the same. Any idea how to amend my code to do this? Cheers