I am fairly new to Python and I am trying to understand how list comprehension works. I start with a list of nested lists (lis1), make a copy using list comprehension to give lis2 (in the function scan()). I use a for loop on lis2, to change the first item of each nested list. When I finally print the original list (lis1) I find that the changes to lis2 have mapped back onto the original list. How does this work?
lis1 = [[1,2],[3,4],[5,6],[7,8]]
def scan():
lis2 = [i for i in lis1] #list comp
print "lis2 copy:",lis2
for x in lis2: #loops across new list to change first item in nested lists
x[0] = 333 #to new val 333
print "lis2 changed:",lis2
print
print "original lis1:",lis1
scan()
print "lis1 also changed:",lis1
>>>
original lis1: [[1, 2], [3, 4], [5, 6], [7, 8]]
lis2 copy: [[1, 2], [3, 4], [5, 6], [7, 8]]
lis2 changed: [[333, 2], [333, 4], [333, 6], [333, 8]]
lis1 also changed: [[333, 2], [333, 4], [333, 6], [333, 8]]