You can do this with this code:
def generate_matrix(size):
"""Generate lists which form an eye matrix"""
for i in range(size):
# First, we create a new list with size 0s.
l = [0] * size
# At the specified position, we place a 1.
l[i] = 1
# We yield this new list.
yield l
It forms a generator which yields the given number of lists, each having the 1 at an advancing position.
3
solved Creating lists with alternate 0 and 1 as generator in Python