#help python arrays
45 messages · Page 1 of 1 (latest)
The code after the loop will run
huh
yes but
i need to detect INSIDE
the for loop
constantly everytime it goes through an index
if its checked all indexes
You'll need to give more information
ok
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
code that checks if x is on its last index
or has gone through all the indexes
!exec ```py
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
code that checks if x is on its last index
print("all indexes done")
@candid geyser
apple
banana
cherry
all indexes done
Oh you want the last one?
i mean inside
the for loop
not outside
of it
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
here
not here
if the code knows
that the last one
is the index its currently on
then that means its passed all indexes
fruits = ["apple", "orange", "blueberry"]
for i, f in enumerate(fruits):
if (len(fruits) - 1) == i:
print(f)
whats len
take a guess 😉
length?
i dont get
how this
shows that its passed all indexes
whats i and f represent also
and enumerate
how do you know if you're done counting grains of rice?
you know because the last grain of rice you counted was the last one in the pile.
same logic
ok
enumerate takes a list, and returns tuples (i, j), where i is the index, and j is the element
tuples?
like an ordered pair
ok
Why is this necessary? This feels like an xy question
oh my days why do people have to ask why its necessary, theres a reason why the user wants it specifically like that and its not your concern, just answer to the question with the given context
i see this on many forms "why is this necessary" the user didnt want you to give outside advice the user wanted you to answer the question
Because sometimes people with more experience will know a better way to do the same thing.
Asking about your attempted solution rather than your actual problem