python - Get Value from list using foreach -


i trying value list using each:

for therepot, member in enumerate(pots[0]):         therepotvalue = therepot 

pots[0] holds [4, 6, 2, 1, 8, 9]

edit


to return value should point variable therepotvalue member , not therepot witch index.

running test:

therepot = 0 , member = 4

therepot = 1 , member = 6

therepot = 2 , member = 2

therepot = 3 , member = 1

therepot = 4 , member = 8

therepot = 5 , member = 9

i think these examples want do:

lst = pots[0]  # solution using loop i, member in enumerate(lst):     # position in list     # member data item list     assert lst[i] == member  # cannot ever fail     if member == the_one_we_want:         break  # exit loop, variables , member set else:     # the_one_we_want never found     = -1  # signal never found  # solution using .index() method function on list try:     = lst.index(the_one_we_want) except valueerror:     # the_one_we_want not found in lst     = -1  # signal never found 

edit: comments made me realize else in for loop confusing.

in python, for loop can have own else case. raymond hettinger has commented wishes keyword had been when_no_break because time use else break keyword!

if for loop exits early, break, else code not run. if for loop runs way end , no break ever happens, @ end else code runs. nick coghlan calls "completion clause" distinguish "conditional else" if statement.

https://ncoghlan_devs-python-notes.readthedocs.org/en/latest/python_concepts/break_else.html

it's sort of unfortunate else comes right after if statement, because might confusing. else has nothing if; goes for loop, why indents way does. (i in python forced line things when go together.)


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -