# File: Programs/ChapterLists/listCompare.py """ Mutable sequences L[i] = x item i of s is replaced by x L.copy() creates a shallow copy of s (same as L[:]) (5) L.pop([i]) ----retrieves the item at i and also removes it from s (2) L.remove(x) ---remove the first item from s where L[i] == x (3) s + t --- the concatenation of s and t (6)(7) s * n or n * s --- n shallow copies of s concatenated (2)(7) >>> r = range(0, 20, 2) # range(startInt,endInt,step) >>> list(range(10)) -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> L=[['a','b'],[1,2]] and >>> L[0][1] -> 'b' >>> Ls = [[]] * 3 -> >>> Ls is [[], [], []] >>> L = [[] for i in range(2)] #makes [[],[]] >>> L[0].append(3) >>> L[1].append(5) >>> L -> [[3], [5], [7]] "A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. "A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original." """ L = [1, 2, 3] M = L #whatever happens to L happens to M N = L.copy() # N is a new list has same initial value as L L.pop() # modify just L but M gets also modifyed!! L = [1,2] print("original list L= ", L, "M = ", M, "N with copy keeps original!") x = 5 L.append(x) # L1 = [1,2,5] modifies L ! # N is the same