def remove_list_duplicates(some_list): '''returns a new list that has all the duplicates removed''' return list(set(some_list)) def find_duplicates(some_list): '''returns a set of the numbers that appear at least twice in some_list. Try using two sets: one for all the numbers and one for the duplicates (along with a loop).''' elem_set = set() # all the elements in the list dups_set = set() # just the duplicates for num in some_list: if num in elem_set: # check if we've already seen num dups_set.add(num) else: # first time we've seen num elem_set.add(num) return dups_set def count_beans(filename): '''Return a list giving the number of beans of each color recorded in filename. Each entry in the list is a two-element list of the bean color and the number of occurrences of that color.''' color_counts = [] data_file = open(filename) for line in data_file: color = line.strip() # throw away the ending newline (\n) color_found = False # whether I've located the right color # look through color_counts for color for entry in color_counts: # check if the color of entry is the right one if entry[0] == color: # this is the color we want, increment counter entry[1] += 1 color_found = True if not color_found: # we didn't find the right color; this is the # first bean of this color -> add a new entry to list color_counts.append([color, 1]) data_file.close() return color_counts