0
cows = ["aaa","aab","aac","aad","aae","aaf","aag","aah","aai"]
h = ["aaa","aab","aac","aad","aae","aaf","aag","aah","aai"]
test1 = []
day1 = []
day2 = []
day3 = []
day4 = []
day5 = []
day6 = []
day7 = []
aaa = []
days = ["Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7"]
w = ["Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7"]
print("Here are your cows :")
print(h)
print("You will need to input the total liters the cow has milked during the day starting from cow aaa to aai.")
for x in range(7):
    print(days[0], end = " ")
    days.pop(0)
    cows = h[0:9]
    for x in range(9):
        print("Cow : ", cows[0])
        test1.append(float(input("How many liters did you milk the cow? ")))
        cows.pop(0)
aaa = test1
for x in range(8):
    aaa.pop(1)
for x in range(8):
    aaa.pop(2)
for x in range(8):
    aaa.pop(3)
for x in range(8):
    aaa.pop(4)
for x in range(8):
    aaa.pop(5)
for x in range(8):
    aaa.pop(6)
for x in range(8):
    aaa.pop(7)

When the code is ran and all the data is input. I try and check the lists

>>> aaa
[20.0, 1.0, 20.0, 20.0, 20.0, 20.0, 20.0]

But then when I type in test1 this happens.

>>> test1
[20.0, 1.0, 20.0, 20.0, 20.0, 20.0, 20.0]

Am I doing something wrong with the lists? I can copy the list test1 into another list "in this case aaa" but then when I try to remove parts of the list from aaa it removes it from test1 also!

El pupper
  • 23
  • 6

2 Answers2

1

With aaa = test1, you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both aaa and test1 refer to the same list after the assignment.

You can do this:

aaa= list(test1)

Possible Duplicate : How to clone or copy a list?

Harshit kyal
  • 365
  • 1
  • 5
  • 24
0

aaa = test1 does not "copy the list", it assigns the same list to another variable. You now have two variables (aaa and test1) that reference the same list, so any manipulation you perform via one of them (e.g., appending elements), will of course also be visible from the other. If you want to copy the contents of test1 to a new list and assign that list to aaa, you could use slicing: aaa = test1[:].

Mureinik
  • 297,002
  • 52
  • 306
  • 350