5.1 Lists like arrays # The first element is a number, the second a string, and the third element is another list. x = [2, "two", [1, 2, 3]] >>> x = [2, "two", [1, 2, 3]] >>> len(x) 3 5.2 List indices >>> x = ["first", "second", "third", "fourth"] >>> x[0] 'first' >>> x[2] 'third' >>> a = x[-1] >>> a 'fourth' >>> x[-2] 'third' >>> x = ["first", "second", "third", "fourth"] >>> x[1:-1] ['second', 'third'] >>> x[0:3] ['first', 'second', 'third'] >>> x[-2:-1] ['third'] >>> x[-1:2] [] >>> x[:3] ['first', 'second', 'third'] >>> x[2:] ['third', 'fourth'] >>> y = x[:] >>> y[0] = '1 st' >>> y ['1 st', 'second', 'third', 'fourth'] >>> x ['first', 'second', 'third', 'fourth'] 5.3 Modifiying lists >>> x = [1, 2, 3, 4] >>> x[1] = "two" >>> x [1, 'two', 3, 4] >>> x = [1, 2, 3, 4] >>> x[len(x):] = [5, 6, 7] #Append a list to the end of a list. >>> x [1, 2, 3, 4, 5, 6, 7] >>> x[:0] = [-1,0] #Append a list to the front of a list. >>> x [-1, 0, 1, 2, 3, 4, 5, 6, 7] >>> x[1:-1] = [] #Remove elements from a list. >>> x [-1, 7] >>> x = [1, 2, 3] >>> x.append("four") >>> x [1, 2, 3, 'four'] >>> x = [1, 2, 3, 4] >>> y = [5, 6, 7] >>> x.append(y) >>> x [1, 2, 3, 4, [5, 6, 7]] >>> x = [1, 2, 3, 4] >>> y = [5, 6, 7] >>> x.extend(y) >>> x [1, 2, 3, 4, 5, 6, 7] >>> x = [1, 2, 3] >>> x.insert(2, "hello") >>> print(x) [1, 2, 'hello', 3] >>> x.insert(0, "start") >>> print(x) ['start', 1, 2, 'hello', 3] >>> x = [1, 2, 3] >>> x.insert(-1, "hello") >>> print(x) [1, 2, 'hello', 3] >>> x = ['a', 2, 'c', 7, 9, 11] >>> del x[1] >>> x ['a', 'c', 7, 9, 11] >>> del x[:2] >>> x [7, 9, 11] >>> x = [1, 2, 3, 4, 3, 5] >>> x.remove(3) >>> x [1, 2, 4, 3, 5] >>> x.remove(3) >>> x [1, 2, 4, 5] >>> x.remove(3) Traceback (innermost last): File "", line 1, in ? ValueError: list.remove(x): x not in list >>> x = [1, 3, 5, 6, 7] >>> x.reverse() >>> x [7, 6, 5, 3, 1] 5.4 Sorting lists >>> x = [3, 8, 4, 0, 2, 1] >>> x.sort() >>> x [0, 1, 2, 3, 4, 8] >>> x = [2, 4, 1, 3] >>> y = x[:] >>> y.sort() >>> y [1, 2, 3, 4] >>> x [2, 4, 1, 3] >>> x = ["Life", "Is", "Enchanting"] >>> x.sort() >>> x ['Enchanting', 'Is', 'Life'] >>> x = [1, 2, 'hello', 3] >>> x.sort() Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: str() < int() >>> x = [[3,5], [2,9], [2,3], [4,1], [3,2]] >>> x.sort() >>> x [[2, 3], [2, 9], [3, 2], [3, 5], [4, 1]] 5.4.1 Custom Sorting >>> def compare_num_of_chars(string1): ... return len(string1) >>> word_list = ['Python', 'is', 'better', 'than', 'C'] >>> word_list.sort() >>> print(word_list) ['C', 'Python', 'better', 'is', 'than'] >>> word_list = ['Python', 'is', 'better', 'than', 'C'] >>> word_list.sort(key=compare_num_of_chars) >>> print(word_list) ['C', 'is', 'than', 'Python', 'better'] 5.4.2 The sorted function >>> x = (4, 3, 1, 2) >>> y = sorted(x) >>> y [1, 2, 3, 4] 5.5.1 in operator >>> 3 in [1, 3, 4, 5] True >>> 3 not in [1, 3, 4, 5] False >>> 3 in ["one", "two", "three"] False >>> 3 not in ["one", "two", "three"] True 5.5.2 List concatenation >>> z = [1, 2, 3] + [4, 5] >>> z [1, 2, 3, 4, 5] 5.5.3 List initialization >>> z = [None] * 4 >>> z [None, None, None, None] >>> z = [3,1] * 2 >>> z [3, 1, 3, 1] 5.5.3 List minimum/maximum >>> min([3, 7, 0, -2, 11]) -2 >>> max([4, "Hello",[1, 2]]) Traceback (most recent call last): File "", line 1, in max([4, "Hello",[1, 2]]) TypeError: unorderable types: str() > int() 5.5.3 List search >>> x = [1, 3, "five", 7, -2] >>> x.index(7) 3 >>> x.index(5) Traceback (innermost last): File "", line 1, in ? ValueError: list.index(x): x not in list 5.5.3 List matches >>> x = [1, 2, 2, 3, 5, 2, 5] >>> x.count(2) 3 >>> x.count(5) 2 >>> x.count(4) 0 5.6 nested lists >>> m = [ [0,1,2], [10,11,12], [20,21,22] ] >>> m[0] [0, 1, 2] >>> m[0][1] 1 >>> m[2] [20, 21, 22] >>> m[2][2] 22 >>> nested = [0] >>> original = [ nested, 1] >>> original [[0], 1] >>> nested[0] = 'zero' >>> original [['zero'], 1] >>> original[0][0] = 0 >>> nested [0] >>> original [[0], 1] >>> original = [ [0], 1] >>> shallow = original[:] >>> import copy >>> deep = copy.deepcopy(original) >>> shallow[1] =2 >>> shallow [[0], 2] >>> original [[0], 1] >>> shallow[0][0] = 'zero' >>> original [['zero'], 1] >>> deep[0][0] = 5 >>> deep [[5], 1] >>> original [['zero'], 1] 5.7.1 Tuple basics >>> x = ('a', 'b', 'c') # Create a three-element tuple. >>> x[2] 'c' >>> x[1:] ('b', 'c') >>> len(x) 3 >>> max(x) 'c' >>> min(x) 'a' >>> 5 in x False >>> 5 not in x True >>> x[2] = 'd' Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> x + x ('a', 'b', 'c', 'a', 'b', 'c') >>> 2 * x ('a', 'b', 'c', 'a', 'b', 'c') >>> x[:] ('a', 'b', 'c') >>> x * 1 ('a', 'b', 'c') >>> x + () ('a', 'b', 'c') >>> x = 3 >>> y = 4 >>> (x + y) # This just adds x and y. 7 >>> (x + y,) # Including a comma indicates the parentheses denote a tuple. (7,) >>> () # To create an empty tuple, just use an empty pair of parentheses. () 5.7.2 Tuple packing >>> (one, two, three, four) = (1, 2, 3, 4) >>> one 1 >>> two 2 >>> x = (1 ,2, 3, 4) >>> a, b, *c = x >>> a, b, c (1, 2, [3, 4]) >>> a, *b, c = x >>> a, b, c (1, [2, 3], 4) >>> *a, b, c = x >>> a, b, c ([1, 2], 3, 4) >>> a, b, c, d, *e = x >>> a, b, c, d, e (1, 2, 3, 4, []) >>> [a, b] = [1, 2] >>> [c, d] = 3, 4 >>> [e, f] = (5, 6) >>> (g, h) = 7,8 >>> i, j = [9, 10] >>> k, l = (11, 12) >>> a 1 >>> [b, c, d] [2, 3, 4] >>> (e, f, g) (5, 6, 7) >>> h, i, j, k, l (8, 9, 10, 11, 12) 5.7.2 Converting Tuples >>> list((1, 2, 3, 4)) [1, 2, 3, 4] >>> tuple([1, 2, 3, 4]) (1, 2, 3, 4) >>> list("Hello") ['H', 'e', 'l', 'l', 'o'] 5.8 Sets >>> x = set([1,2,3,1,3,5]) >>> x {1, 2, 3, 5} >>> x.add(6) >>> x {1, 2, 3, 5, 6} >>> x.remove(5) >>> x {1, 2, 3, 6} >>> 1 in x True >>> 4 in x False >>> y = set([1,7,8,9]) >>> x | y {1, 2, 3, 6, 7, 8, 9} >>> x & y {1} >>> x ^ y {2, 3, 6, 7, 8, 9} >>> 5.8.2 Frozensets >>> x = set([1,2,3,1,3,5]) >>> z = frozenset(x) >>> z frozenset({1, 2, 3, 5}) >>> z.add(6) Traceback (most recent call last): File "", line 1, in z.add(6) AttributeError: 'frozenset' object has no attribute 'add' >>> x.add(z) >>> x {1, 2, 3, 5, frozenset({1, 2, 3, 5})}