I have this code:
philips_trousers = []
for i in range(0, 5):
philips_trousers.append(["T"] * 5)
philips_trousers.append(["R"] * 5)
philips_trousers.append(["O"] * 5)
philips_trousers.append(["U"] * 5)
philips_trousers.append(["S"] * 5)
philips_trousers.append(["E"] * 5)
philips_trousers.append(["R"] * 5)
philips_trousers.append(["S"] * 5)
print philips_trousers
Which outputs the following:
[['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S'], ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S'], ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S'], ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S'], ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S']]
So far so good you're probably thinking, but I'm wondering something -- why does Python fill philips_trousers in quite the way it does? That is, why does the append() function create new child arrays with the form ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'] etc, rather than: ['T', 'R', 'O', 'U', 'S', 'E', 'R', 'S']?
['T', 'R', 'O', 'U', 'S', 'E', 'R', 'S']?appendon thephilips_trouserslist, not on its sublists. You're not going to append to the list's sublists.['R', 'R', 'R', 'R', 'R']) and begin another (say['O', 'O', 'O', 'O', 'O'])? That's the bit which doesn't make sense to me. What instruction am I passing to the compiler that causes it to end one child array and begin another?["T"] * 5is one list.["R"] * 5is another. Nothing here would stick them together.