""" Lecture 5/6 Demo'd code exploring lists and different ways to define and use them. """ # While not suggested, you can mix types in a list lst = [1, '0.5', -2, min, 0.5, 'Curly', [0, 1, 2]] # Note above that `lst` is an ok variable name # to refer to some arbitrary list; don't use # `list` as a variable name (it's a built-in Python # type) and use more meaningful variable names if # appropriate. # Average temps from Sept. 30 to Oct. 6 in Pasadena temps = [87.4, 87.5, 82.9, 80.3, 79.5, 78.3, 76.3] # Accessing elements temps[0] temps[2] temps[-1] # temps[len(temps) - 1] # Two ways to use brackets: creating and accessing [1, 2, 3, 4, 5][2] # (rarely see this in practice) days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] days_in_months[0] days_in_months[1] # How do we get the number of days in October (Month 10)? (2 ways) # Can also access from the end days_in_months[-1] # last element (Dec.) days_in_months[-2] # second-to-last element (Nov.) # Careful, accessing at an index outside of the bounds gives an error # days_in_months[len(days_in_months)] # last element: days_in_months[11] # days_in_months[-13] # first element: days_in_months[-12] # Modifying lists nums = [-1, 12, 4, 5, 2] nums[2] = 8 # How is this different than the string sequence? s = 'Lorem' s[0] = 'l' nums[2] = 2 * nums[2] # nums[2] = 2 * 4 # nums[2] = 8 nums[2] *= 2 nums[2] = nums[2] * 2 # Operators on lists # Concatenation with + [1, 2, 3] + [4, 5, 6] # Replication with * [1, 2, 3] * 3 [1, 2, 3] * 0 [1, 2, 3] * -1 # List functions len([1, 2, 3]) len(days_in_months) days_in_months[int(len(days_in_months) / 2) + 2] # days_in_months[len(days_in_months) // 2 + 2] # Can convert another sequence to a list (if possiible) curly_lst = list('Curly') # And can convert a list back to aa string (delimited by the # string .join is called on). Don't overuse though (e.g. # don't take a string, convert to list, then join back to a string). curly_str = ''.join(curly_lst) # We can add to the end of a list with append nums = [] nums.append(1) nums.append(2) nums.append(3) nums.append(2) # To find an element index (location) in a list, use the index method nums.index(2) # index of the _first_ occurrence of the value 2 # error if value isn't in list # nums.index(4) nums = [1, 2, 3, 2, 4] # If there are multiple occurrences of the value, returns the earliest index nums.index(2) # To remove an element, use the remove method nums.remove(2) # Only removes first element of the list, and error if not found # Summing a list sum(temps) # Finding the average sum(temps) / len(temps) # Reversing a list nums.reverse() # Aliasing # Assigning a list to a variable does not copy that list. # Instead, it is a reference to that value, and the value # update will be seen in both variables (show debugger) nums = [-1, 12, 4, 5, 2] nums2 = nums nums2[0] = 0 print(nums2) print(nums) # Compare this with strings and ints x = 1 y = x x *= 2 print(x, y) s1 = 'foo' s2 = 'bar' s1 *= 2 print(s1, s2) # Can also make copies if you don't want aliasing nums2 = nums.copy() nums2[0] = 0 print(nums2) print(nums) # Slicing # Can select a group of elements in the list days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # lst[start:end] -> returns a list holding items between [start, end) days_in_months[1:] # everything after the first element days_in_months[1:3] # elements 2nd to 3rd days_in_months[:-4] # elements beginning to 4th-to-last element # Can also change a sequence of items using splice nums = [1, 2, 3, 4, 5] nums[1:3] = [0, 0] cities = ['Pasadena', 'Los Angeles', 'Sacramento', 'San Diego', 'San Francisco'] for city in cities: print(city) print('---') temp = 85 if temp > 72: print('Hot!') elif temp > 80: print('Hotter!') rna_seq = 'ugcca' # expected result: ['a', 'c', 'g', 'g', 'u'] # make a list result (rna_lst) for ch in rna_seq: # add the complement of ch in the list if ch == 'a': ... elif ch == 'c': ... elif ch == 'g': ... else: # ch == 'u' ... # Can get the count of items in a list nums.count(2) # And sort them. Remember that lists are mutable! nums.sort() # list_cyu.py Exercise # Practice with functions # Exercise: Write a function get_median that returns the median of the list # Assume the list is an odd-length (why?) def get_median(lst): temp = lst.copy() temp.sort() middle_index = len(temp) // 2 return temp[middle_index] nums = [1, -1, 4, 18, 2, 0, 5] median = get_median(nums) # Exercise (home): Write a function get_range that returns the # range of the list, where range = max - min def get_range(lst): return 0 # Exercise (at home): Write a function vowel_count that returns a list of vowel counts def vowel_count(s): # e.g. vowel_count('bOwie') should return: # [0, 1, 1, 1, 0] # representing counts for: # ['a', 'e', 'i', 'o', 'u'] return 0 # Nested lists # The following puzzles are from Reading 8, practicing nested lists # List Puzzle 1 # Can you explain why the following code # behaves the way it does? Try with the VSCode debugger or PythonTutor! lst1 = [1, 2, 3] lst2 = [lst1, lst1] print(lst2) lst1[0] = 42 print(lst1) print(lst2) # List Puzzle Part 2 # What does the following code do? lst2[0][0] = 1 print(lst2) print(lst1) # List Puzzle Part 3 # What does the following code do? lst1 = [1, 2, 3] lst2 = lst1 * 3 print(lst2) lst3 = [lst1] * 3 print(lst3) lst1[0] = 42 print(lst1) print(lst2) print(lst3) lst1 = [1, 2, 3] lst2 = [lst1, lst1] print(lst2) lst1[0] = 42 print(lst1) print(lst2) # Challenge problem (on your own) def mystery(lst, x, y): lst[lst[y]] = lst[x] lst[x] = y nums = [1, -2, 6, 4, 0, 3, 0, 9, 12] mystery(nums, 2, 4) # use the debugger to "step into" this call # What does nums look like here? nums