I am trying to write a code that generates a pattern of L's and R's. I've written a while loop that writes the pattern, but when I attempt to add the next iteration of the pattern to the list, multiple elements are altered. I've pasted the code below.
When I print "mostrecentpattern", I do get the correct result, and my goal is to add that mostrecentpattern as a new element onto the list already existing list (called pattern). I've also included a photo of the result - each element in the list, starting with the second element, is altered when I append.enter image description here
list1 = ["L"]
list2 = ["R"]
# beginning of the pattern
pattern = [list1, list2]
iterations = 0
while iterations < 2:
i = 0
# this is getting the most recent element of the pattern
mostrecentpattern = pattern[len(pattern) - 1]
# getting number of iterations
length = len(mostrecentpattern)
print(length)
while i <= length - 1:
if mostrecentpattern[i] == "L":
mostrecentpattern.append("R")
else:
mostrecentpattern.append("L")
i += 1
print("most recent pattern")
print(mostrecentpattern)
print(pattern)
pattern.append(mostrecentpattern)
print("pattern after appending")
print(pattern)
iterations += 1