Question: I have a list of randomly generated 0's and 1's. I need to pick a single bit and flip it to its opposite: either a zero to a one or a one to a zero. 
The bitwise not operator only works on integers and long integers, and the xor (`^`) operator works with two integers.

```
Enter a population size: 4
Enter an organism length: 2
[[1, 1], [0, 1], [0, 0], [0, 0]]
[[[1, 1], [0, 1]]]

```

The code above is part of a short user input program where the user inputs pop size and length. The program prints a list of randomly generated numbers with mind to pop size and length and takes the top 50%, which is the second printed list. Now I need to pick a random bit from the second list and flip it to either a zero or a one. Not the whole list, however. 

Links and explanations are much appreciated, I'm looking to improve.

Answer: 
You can use `randint` to generate a random integer for the index, and then use for instance `^1` to flip it, like:

```
from random import randint

pop = [[1, 1], [0, 1], [0, 0], [0, 0]]
individual = pop[1]                            # select the second individual
individual[randint(0,len(individual)-1)] ^= 1  # flip the bit

```

After I ran this, I obtained:

```
>>> pop
[[1, 1], [0, 0], [0, 0], [0, 0]]

```

so it flipped the second bit. But it could have been the first as well. By using `len(individual)` we guarantee that if the number of bits of the individual increases, it will still work.

That been said encoding bits as 0-1s in a list, is not very efficient. You can use `int`s in Python as a list of bits (`int`s have arbitrary length in [python-3.x](/questions/tagged/python-3.x "show questions tagged 'python-3.x'")).

**EDIT**

If you want to flip a bit *per* individual (for every individual it can be a *different* bit), you can use a `for` loop:

```
for individual in pop:                             # iterate over population
    individual[randint(0,len(individual)-1)] ^= 1  # flip a random bit

```

If I run this with your given initial population, I get:

```
>>> pop
[[0, 1], [1, 1], [0, 1], [1, 0]]

```

so every individual has exactly one bit flipped, and not all the same one. Of course it is random, so it is possible that at a certain run, the random number generator will pick the same bit for every individual.
To flip a single bit, use logic arguments. 

```
int(not 0) = 1
int(not 1) = 0

```

If you have to flip exactly one bit in every population, I would suggest:

```
chrom = random.choice(pop)
j = random.randrange(len(chrom))
chrom[j] = int(not chrom[j])

```