I am very new to learning Python as I have just moved from Lua. One of my questions though, is how do I iterate over a table with a given set of different values? I have tried looking on other forums, but still do not understand and would like the easiest solution possible, as well explained.
For example, I have a table of numbers, and would like to iterate through that table, printing both the key and the element of the table. How would I do this in Lua?
This is what I mean when written in Lua:
local table = {1, 3, 5, 7;}
for i,v in pairs(table) do
print(v)
end
I think you are trying to do this:
table = [1, 3, 5, 7] # create the list
for v in table: # going through all elements of the list
print(v) # print the element
If you want to have the value and the index when going through the list, you can use enumerate
like this:
table = [1, 3, 5, 7] # create the list
for i, v in enumerate(table): # going through all elements of the list with their indexes
print(i) # print the element index
print(v) # print the element value