"Sorting Lists", What does item[1] stand for here?

Hi,

What does the [1] stand for in this code:

items = [
    ("Product1", 10),
    ("Product2", 9),
    ("Product3", 12)
]


def sort_item(item):
    return item[1] 


items.sort(key=sort_item)
print(items)

Or in this one:

items = [
    ("Product1", 10),
    ("Product2", 9),
    ("Product3", 12)
]


items.sort(key=lambda item:item[1])
print(items)

Mosh says it will “return item of 1,” still don’t know what 1 is, as 2 doesn’t work and 0 returns the original list order. I thought maybe it was a boolean, like True or False situation, but it says it’s an integer.

Note: The code works, I just want to understand HOW it works.

Thanks!

I rewatched the video about 3 times, and now I’m thinking it must be the position inside each tuple, which has 0 and 1 only. Rookie mistake.

yes, you are correct

1 Like

Basically if you iterate through the dictionary, it always returns the keys. As you know eg:- for evi in dict:, evi is the key. Now to get the value for that key, you have to locate that key in the dictionary. so you locate the key with this syntax dict[key], this will return the value for that key. in the above eg: items[Product1] will give you 10.
Hope this helps