Functions ========= Run the following code cell, which will load from the file ``data.py`` a list of 100 names. .. code:: ipython3 from data import names_list **Define a function called ``add_user`` with two parameters, called ``users`` and ``name``, and does the following:** - ``.append()`` ``name`` to ``users`` (which can be assumed to be a list) - ``print()`` the message ‘```` added successfully!’ .. code:: ipython3 def add_user(users, name): users.append(name) print(f'{name} added successfully!') Check that the following code works as expected: .. code:: ipython3 members = ['Helen', 'Sam'] add_user(members, 'John') members .. parsed-literal:: John added successfully! .. parsed-literal:: ['Helen', 'Sam', 'John'] **Define a function called ``unique`` which takes a list and returns a ``set()`` of the unique values in that list**: - avoid using ``list`` as a parameter name, because it already used by Python; give the parameter a different name, such as ``user_list`` **Note:** *Here and elsewhere in the notebook, by ‘unique’ it is meant ‘de-duplicated’, rather than a count / list / set of the values which only occur once in the given object.* .. code:: ipython3 def unique(user_list): return set(user_list) .. code:: ipython3 unique(['Bill', 'Betty', 'Bill', 'Arthur', 'Mildred']) .. parsed-literal:: {'Arthur', 'Betty', 'Bill', 'Mildred'} **Once you are confident that your ``unique()`` function works as expected, call it using ``names_list`` (which we imported earlier) and assign the resulting ``set`` to ``names_set``:** .. code:: ipython3 names_set = unique(names_list) .. code:: ipython3 print(names_set) .. parsed-literal:: {'Christine', 'Stewart', 'Marion', 'Rachael', 'Carol', 'Leigh', 'Gerard', 'Kathryn', 'Shaun', 'Clive', 'Simon', 'Beverley', 'Gareth', 'Georgina', 'Carole', 'Gavin', 'Brian', 'Marian', 'Mohamed', 'Amy', 'Joanne', 'Colin', 'Adrian', 'Karen', 'Jean', 'Christian', 'Sean', 'Lindsey', 'Judith', 'Justin', 'Jeffrey', 'Grace', 'Francis', 'Beth', 'Paula', 'Leon', 'Elliot', 'Jack', 'Ryan', 'Darren', 'Jennifer', 'Abigail', 'Shirley', 'Charlene', 'Gillian', 'Brett', 'Jessica', 'Jenna', 'Alan', 'Donald', 'Teresa', 'Stephanie', 'Josh', 'Charles', 'Kathleen', 'Ricky', 'Jade', 'John', 'Dorothy', 'Kelly', 'Frances', 'Angela', 'Ellie', 'Albert', 'Kayleigh', 'Jamie', 'Dale', 'Hugh', 'Sian', 'Danielle', 'Gail', 'Robert', 'George', 'Adam', 'Lee', 'Jake', 'Andrew', 'Kyle', 'Dean', 'Marilyn', 'Samantha', 'Nicole', 'Lynne', 'Ann', 'Graham', 'James', 'Sharon', 'Jeremy'} **Define a function called ``duplicate_count`` which returns the number of duplicate values in a list:** - call your ``unique()`` function within the ``duplicate_count()`` function - remember to avoid using a parameter name which has been used as a variable name elsewhere *i.e., how many values in the list are equal to another value in the list.* .. code:: ipython3 def duplicate_count(dupe_list): all_count = len(dupe_list) unique_count = len(unique(dupe_list)) dupe_count = all_count - unique_count return dupe_count .. code:: ipython3 duplicate_count(names_list) .. parsed-literal:: 12 | **Define a function called ``names_dict``, which takes a list and returns a dictionary where:** - there is a key for each unique first character found in the list | - the values are a list of the entries which begin with the character used for the key - use the built-in ``sorted()`` function to order the values lists alphabetically .. code:: ipython3 def names_dict(names): first_chars = [] for name in names: first_chars.append(name[0]) keys = sorted(set(first_chars)) #sorted() not strictly necessary here new_dict = dict() for k in keys: name_list = [] for name in names: if name[0] == k: name_list.append(name) new_dict[k] = sorted(name_list) return new_dict **Assign to ``all_names_dict`` the value returned from calling ``names_dict`` with ``names_list``:** .. code:: ipython3 all_names_dict = names_dict(names_list) all_names_dict .. parsed-literal:: {'A': ['Abigail', 'Abigail', 'Adam', 'Adrian', 'Adrian', 'Alan', 'Albert', 'Amy', 'Andrew', 'Andrew', 'Angela', 'Ann'], 'B': ['Beth', 'Beverley', 'Brett', 'Brian'], 'C': ['Carol', 'Carole', 'Charlene', 'Charlene', 'Charlene', 'Charles', 'Christian', 'Christine', 'Clive', 'Colin'], 'D': ['Dale', 'Danielle', 'Darren', 'Dean', 'Donald', 'Dorothy'], 'E': ['Ellie', 'Elliot', 'Elliot'], 'F': ['Frances', 'Francis'], 'G': ['Gail', 'Gareth', 'Gavin', 'George', 'Georgina', 'Gerard', 'Gillian', 'Grace', 'Graham'], 'H': ['Hugh'], 'J': ['Jack', 'Jade', 'Jade', 'Jake', 'James', 'Jamie', 'Jean', 'Jeffrey', 'Jenna', 'Jennifer', 'Jennifer', 'Jennifer', 'Jeremy', 'Jessica', 'Joanne', 'John', 'Josh', 'Judith', 'Justin'], 'K': ['Karen', 'Kathleen', 'Kathleen', 'Kathryn', 'Kayleigh', 'Kelly', 'Kyle'], 'L': ['Lee', 'Leigh', 'Leon', 'Lindsey', 'Lindsey', 'Lynne'], 'M': ['Marian', 'Marilyn', 'Marion', 'Mohamed'], 'N': ['Nicole'], 'P': ['Paula', 'Paula'], 'R': ['Rachael', 'Ricky', 'Robert', 'Ryan'], 'S': ['Samantha', 'Sean', 'Sharon', 'Shaun', 'Shirley', 'Sian', 'Simon', 'Stephanie', 'Stewart'], 'T': ['Teresa']} **Define a function called ``unique_names_dict`` which takes a list and returns a dictionary such as the one created above, but instead of lists the values are sets (of unique entries):** - Use the ``names_dict`` and ``unique`` functions previously defined in your solution - You may find the ``.items()`` dictionary method useful .. code:: ipython3 def unique_names_dict(names): all_dict = names_dict(names) new_dict = dict() for item in all_dict.items(): key = item[0] val = set(item[1]) new_dict[key] = val return new_dict .. code:: ipython3 unique_names_dict(names_list) .. parsed-literal:: {'A': {'Abigail', 'Adam', 'Adrian', 'Alan', 'Albert', 'Amy', 'Andrew', 'Angela', 'Ann'}, 'B': {'Beth', 'Beverley', 'Brett', 'Brian'}, 'C': {'Carol', 'Carole', 'Charlene', 'Charles', 'Christian', 'Christine', 'Clive', 'Colin'}, 'D': {'Dale', 'Danielle', 'Darren', 'Dean', 'Donald', 'Dorothy'}, 'E': {'Ellie', 'Elliot'}, 'F': {'Frances', 'Francis'}, 'G': {'Gail', 'Gareth', 'Gavin', 'George', 'Georgina', 'Gerard', 'Gillian', 'Grace', 'Graham'}, 'H': {'Hugh'}, 'J': {'Jack', 'Jade', 'Jake', 'James', 'Jamie', 'Jean', 'Jeffrey', 'Jenna', 'Jennifer', 'Jeremy', 'Jessica', 'Joanne', 'John', 'Josh', 'Judith', 'Justin'}, 'K': {'Karen', 'Kathleen', 'Kathryn', 'Kayleigh', 'Kelly', 'Kyle'}, 'L': {'Lee', 'Leigh', 'Leon', 'Lindsey', 'Lynne'}, 'M': {'Marian', 'Marilyn', 'Marion', 'Mohamed'}, 'N': {'Nicole'}, 'P': {'Paula'}, 'R': {'Rachael', 'Ricky', 'Robert', 'Ryan'}, 'S': {'Samantha', 'Sean', 'Sharon', 'Shaun', 'Shirley', 'Sian', 'Simon', 'Stephanie', 'Stewart'}, 'T': {'Teresa'}} **Define a function ``unique_count()`` which takes a single character and a list, and returns the number of unique entries in the list which begin with that character:** - use the ``unique_names_dict()`` function defined above within ``unique_count()`` - if there are no matching items in the list, return ``0`` and print ``None found`` .. code:: ipython3 def unique_count(char, names): unique_dict = unique_names_dict(names) count = len(unique_dict.get(char, [])) if count == 0: print('None found') return count .. code:: ipython3 unique_count('J', names_list) .. parsed-literal:: 16