Shortcuts

Functions

Run the following code cell, which will load from the file data.py a list of 100 names.

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 ‘<name> added successfully!’

def add_user(users, name):
    users.append(name)
    print(f'{name} added successfully!')

Check that the following code works as expected:

members = ['Helen', 'Sam']
add_user(members, 'John')
members
John added successfully!
['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.

def unique(user_list):
    return set(user_list)
unique(['Bill', 'Betty', 'Bill', 'Arthur', 'Mildred'])
{'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``:

names_set = unique(names_list)
print(names_set)
{'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.

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
duplicate_count(names_list)
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
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``:

all_names_dict = names_dict(names_list)
all_names_dict
{'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

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
unique_names_dict(names_list)
{'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

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
unique_count('J', names_list)
16

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources