letters = [“a”, “b”, “c”]
items = [0, “a”]
index, letter = items
for letter in enumerate(letters):
print(letter[0], letter[1])
Why letter[1] refers to the letters in the list ?
letters = [“a”, “b”, “c”]
items = [0, “a”]
index, letter = items
for letter in enumerate(letters):
print(letter[0], letter[1])
Why letter[1] refers to the letters in the list ?
Hey @Lt.Smith,
Because you used “letters” in the for loop which points to [“a”, “b”, “c”].
BTW “letter”(line 3) is declared as “a” initially but over written in the for loop(line 4). Is that intentional?
I just wrote the code like Mosh, just copy-paste
Which lecture was it?
Looping over Lists from the Data Structures chapter.
But the code that I posted is not the finished code, I just didn’t understand why letter[1] was referring to the letters.
No worry’s, just curious.
Was I able to answer your question?
Yeah, you did. Thank you!
Anytime.
If you run into anything else or just looking for advice, the forums are here for you.
Thank you!Good to know !
Hi again!
Do you know why we need to wrote return item[1] in the bellow code:
items = [(“Product1”, 8),
(“Product2”, 15),
(“Product3”, 6)
]
def sort_item(item):
return item[1]
items.sort(key=sort_item)
print(items)
Sorting lists lesson
If you recall from lists that every item has an index starting with 0. item[1] simply means get the second value. i.e (“Product1”, 8) gets the number 8.
Its not clear but sort has a for loop looping through the items list. gets a tuple(e.x (“Product1”, 8)). Then it passes the tuple into your function"sort_item" and returns the second value. once it gets the second values it sorts it based on the number.
Hi again!
Anther question if you don’t mind:)
What is the difference between a signed integer and an unsigned one?
Here is a stack post that explains it well.
Thank you! That kind of examples really helps. As simple as possible, otherwise my brains boils