-1

in Python how does one call the current position of a interation in a "for x, y, z in list():"?

I have a list with numerous subsists consisting of three variables, but I don't quite know how to call the current position of the loop - ie. Call the value of X in the current iteration of the loop.

I am really new to coding and python so any help would be appreciated greatly - I read something about enumerate but that just confused me and I didn't know whether it would help or how to reference it or use it

currently my loop looks like: for step_num, direction, changes in movements:

with movements being a list consisting of multiple sub lists with three variables each (one numeric and two alphanumeric). my goal is to be able to reference the current variable of the current sublist being iterated through - I'd read something about enumerate potentially being able to help with finding the current value of a sub list variable, however I don't know how to use it as such, if indeed it can be used like that, especially since the output is being used in a turtle window to draw different objects.

As it stands I'm not sure how to make it happen, so the functions making drawings don't know how to draw the current value in the loop

Josh
  • 11
  • 2
  • 4
    What about `enumerate` confused you? – Scott Hunter May 02 '22 at 14:52
  • 1
    Share your code please and explain how the output you're getting differs from the desired output. – timgeb May 02 '22 at 14:53
  • 2
    Please share an example of what you are working with; tell us a bit more in code about what you have tried, and also, as stated above, what about `enumerate` confuses you. – navneethc May 02 '22 at 14:54
  • Does this answer your question? [Accessing the index in 'for' loops](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops) – OTheDev May 02 '22 at 14:58
  • @navneethc Sorry for the lack of clarification, I've updated my post with some of my code and the issues – Josh May 02 '22 at 15:08
  • @oda I don't think so, unless I'm thick in the head today - I forgot to mention that the code is being used in a turtle window – Josh May 02 '22 at 15:08
  • @Josh Apologies. I saw the support for the answer below and recalled seeing a virtually identical answer in the cited post above. – OTheDev May 02 '22 at 15:20

2 Answers2

3

you can use enumerate(list) like so:

for index, y in enumerate(list):
    print(index) # position of loop
    print(y) # item in list
pzutils
  • 490
  • 5
  • 10
  • thank you for the response, I realized I didn't put enough info on my post and so have updated it. :) – Josh May 02 '22 at 15:11
1

I'm making some assumptions here, but are you looking for something like this?

# iterate through each sublist in `movements`
# and also get its index
for ix, movement in enumerate(movements): 
    for step_num, direction, changes in movement: # extract the values from each sublist
        ... # do stuff with the index and corresponding values

P.S.: If you are new to SO, please take sometime to learn how to produce a minimal reproducible example. It will only increase your chances of getting a quick and useful response.

navneethc
  • 1,234
  • 8
  • 17
  • Thank you very much! I'll work in making my questions more easily workable in future. – Josh May 02 '22 at 21:43