Writing Data to Unknown List Size - Python

Please see my attached python script from a larger script I am putting together. This example is simpler than my actual problem that I am trying to solve, but illustrates my lack of understanding of python code:

In the above example, I need to be able to write the value of “test” to the "a"th index of the list “counttest”.

In my more complicated code, I will not know the length of “counttest” and therefore need the length of “counttest” to be populated one by one, possibly being overwritten as it goes.

I am guessing this is where the python code is getting tripped up, any ideas on how to fix? My error is index 0 is out range.


Whoops I posted the wrong image, counttest is supposed to be an empty set,

@austin.guter what’s your desired output?

in this case it would be [1,2,3], just reading the value of test each time it is adding 1 to it.


Thanks sal, but maybe my example is too simplified.

In the end, i do not think that I can use the append function as shown, as I have some conditions on if data gets written to a list. I would love if I could get the above example to work with indices and not the append function.

I can post the more complicated example if it is still not clear.

@austin.guter show your original data and what you would expect

here is the more complicated example where I have adopted the append with a while loop.

list 1 has values that are ascending in order, and I want to get results that spit out the quantity of ascending values.
the end goals should be a list that is [1,3,1,2]

I think i am close with the following code, just struggling to get the last value of “2” to be read:

Sounds like you are better off using a for loop instead:

act_values = []

for ind,val in enumerate(value):
  if ind == len(value)-1:
  elif value[ind+1] > val:

The enumerate function just lets you select the index (ind) and the value (val) at the same time, corresponding to the same iteration of the for loop.

That being said, I wrote this but have no clue what the intention or use case of it is, nor how it reacts to different values. Your explanation didn’t really make sense for me. :thinking:

1 Like

@austin.guter I’m not sure what you mean. is there a real case behind this exercise ?

Reading it for the 15th time I think I am understanding the logic, in which case my python code above probably doesn’t work for it. Let me see if I can write another way.

Edit: So after trying to think about it and drawing it out, I have come to determine that I still have no damn clue what you are trying to solve with this. The behavior makes no sense from your screenshot of python. However, my python above will do the same behavior as yours but also get you the final number you needed.

1 Like

I apologize for all the confusion and truly appreciate everyone’s help!
In the end, i got my code to work adding an additional if statement.
my final code is the following: