# File: ChapterLists/list2.py """ Mutable sequences Check data type: isinstance(L,list) type = str, int, float s[i] ith item of s, origin 0 s[i:j] slice of s from i to j s[i:j:k] slice of s from i to j with step k len(s) length of s x in s True if an item of s is equal to x, else False (1) x not in s False if an item of s is equal to x, else True (1) s + t concatenation of s and t del s[i:j] same as s[i:j] = [] del s[i:j:k] removes the elements of s[i:j:k] from the list s.append(x) appends x to the end of the sequence (same as s[len(s):len(s)] = [x]) s.clear() removes all items from s (same as del s[:]) (5) s.copy() creates a shallow copy of s (same as s[:]) (5) s.extend(t) extends s with the contents of t (same as s[len(s):len(s)] = t) s.insert(i, x) inserts x into s at the index given by i (same as s[i:i] = [x]) s.pop([i]) retrieves the item at i and also removes it from s (2) s.remove(x) remove the first item from s where s[i] == x (3) s.reverse() reverses the items of s in place s * n or n * s n shallow copies of s concatenated (2)(7) min(s) max(s) smallest item of s and largest item of s s.index(x[, i[, j]]) index of the first occurrence of x in s (at or after index i and before index j) (8) s.count(x) total number of occurrences of x in s L = [-1, 3, -10, 2] >>> len(L) -> 4 >>> L[0] -> first -1 >>> L[-1] -> last 2 >>> L[2:4] -> from index 2 till index 3 (only indexes 2 and 3) >>> L[2:] -> till end starting with index 2 [-10,2] >>> L[:2] -> till index 2 [-1,3] >>> L.count(3) -> 1 # counts item 3 in List >>> L.insert(2, 88) -> # L = [-1, 3, 88, -10, 2] inserts item 88 in List in position 2 >>> L.append(7) # L = [-1, 3, -10, 2, 7] append item 7 in List >>> L.index(3) -> 1 # finds index of item 3 >>> L.remove(3) # L = [-1, -10, 2] removes first occurrence of 3 in List changes L >>> L.reverse() # L = [2, -10, 3, -1] reverses the list >>> L.pop() -> 2 # L = [-1, 3, -10] removes last item >>> r = range(0, 20, 2) # range(startInt,endInt,step) >>> r -> range(0, 20, 2) # r=[0,2,4,6,8,10,12,1,4,16,18] >>> 11 in r -> False >>> 10 in r -> True >>> r.index(10) ->5 >>> r[5] -> 10 >>> r[:5]-> range(0, 10, 2) >>> r[-1] -> 18 #last >>> list(range(10)) -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(0, 10, 3)) -> [0, 3, 6, 9] >>> list(range(0)) -> [] >>> L=[['a','b'],[1,2]] -> >>> L[0][1] -> 'b' >>> Ls = [[]] * 3 -> >>> Ls is [[], [], []] >>> Ls[0].append(3)-> >> Ls is [[3], [3], [3]] >>> "gg" in "eggs" -> True >>> L = [[] for i in range(3)] #makes [[],[],[]] >>> L[0].append(3) >>> L[1].append(5) >>> L -> [[3], [5]] """ import random L = [-1, 3, -10, 2] print("original list L= ", L) L2 = [(3 * x) for x in L if (x < 0)] # if x < 0 multiply by 3 and put in a list print("L2 = ", L2) # L2 = [-3, -30] L4 = [x for x in L if (x >= 0)] # select only positive from L (filter) print("L4 = ", L4) # L4 = [3, 2] L3 = [(x + y) for x in L for y in range(1,3)] print("[x+y for x in L for y in range(1,3)] = ", L3) # [7, 11, 12, 12, 17] #>>> [x*y for x in [1,2,3] for y in [0,1]] => [0, 1, 0, 2, 0, 3] Lrand = [ random.randint(1,20) for i in range(5) ] # make a random list length 5 print(Lrand) # Lrand = [3, 13, 14, 8, 18]