#help python arrays

45 messages · Page 1 of 1 (latest)

cyan halo
#

how would you be able to detect in the code when the for loop has looped through all the values of the array

candid geyser
#

The code after the loop will run

cyan halo
#

huh

#

yes but

#

i need to detect INSIDE

#

the for loop

#

constantly everytime it goes through an index

#

if its checked all indexes

candid geyser
#

You'll need to give more information

cyan halo
#

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

candid geyser
#

!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")

modest sandalBOT
candid geyser
#

Oh you want the last one?

cyan halo
#

i mean inside

#

the for loop

#

not outside

#

of it

#

fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

here

not here

cyan halo
#

that the last one

#

is the index its currently on

#

then that means its passed all indexes

cosmic kelp
#
fruits = ["apple", "orange", "blueberry"]
for i, f in enumerate(fruits):
    if (len(fruits) - 1) == i:
        print(f)
cyan halo
#

whats len

cosmic kelp
#

take a guess 😉

cyan halo
#

length?

#

i dont get

#

how this

#

shows that its passed all indexes

#

whats i and f represent also

#

and enumerate

cosmic kelp
#

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

cyan halo
#

ok

cosmic kelp
#

enumerate takes a list, and returns tuples (i, j), where i is the index, and j is the element

cyan halo
#

tuples?

cosmic kelp
#

like an ordered pair

cyan halo
#

ok

grave phoenix
cyan halo
#

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

grave phoenix
#

Because sometimes people with more experience will know a better way to do the same thing.

cosmic kelp