I am trying to understand how genetic algorithms work. As with everything, I learn by attempting to write something on my own;however, my knowledge is very limited and I am not sure if I am doing this right.

The purpose of this algorithm is to see how long it will take half of a herd to be infected by a disease if half of that population are already infected. It is just an example I came up with in my head so I am not sure if this would even be a viable example.

if - for the sake of introductory understanding - you want to apply these concepts to your example:

inheritance:conceptual: you need a genetic identity of your unit (the animal).code: i.e. you need animals with state, different instances.how: each animal inherits this genetic identity to the next generation when it is reproduced.

mutation:conceptual: each generation is slightly different from the other.code: each animal in a new generation has slightly different values than its parent.how: apply some random modification.

selection of fitness:conceptual: you select which units are reproduced for the next generation by the differences between the units. choose the fittest.code: when assembling the new generation, choose only those units for reproduction that satisfy a certain fitness limit (e.g. the best 90%).how: define a fitness-function which returns a value indicating relative fitness for each unit.

to put it together, you could make e.g. different herds of animals that have different starting values for the 'genetic' attributes (e.g. preferred_proximity_to_next_animal, skin_color, height, aggressiveness, etc
.).
Then, in your fitness function, you define how each of the attributes is connected to fitness, i.e. how they are related to the probability to get infected. then let the program run for a some of generations and see how at each iteration the global probability of infection decrease in relation to the genetic setup of your herd.

Like that you might find favourable combinations of attributes.
Generally, genetic algorithms are used in search-related problems.

Its generally not a good idea to catch an IndexError. Its too easy to get an accidental IndexError and then have your code miss the real problem. It also doesn't communicate why you are getting an index error.

Instead of updating pos, I suggest you use

for pos, animal in enumerate(herd):

However, since you don't want the first and last elements anyways, I'd suggest