Question

In: Computer Science

Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...

Create two functions that you can use for a dictionary manager:

1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist;

2. add_new_item(dictionary,key,value): a function that adds a new item to the dictionary (using the input key and value) if there is no existing item in the dictionary with the supplied key. If the input key already exists in the dictionary, print out a message saying that the new item has not been added because there is already a matching key in the dictionary. No Python error should be raised by this fuction if the key already exists.

Test out your functions by creating a dictionary with several items and then doing four (4) things:

  1. removing an item that exist;

  2. removing an item that doesn’t exist;

  3. adding an item whose key doesn’t exist;

  4. adding an item whose key does exist;

Print out the contents of the dictionary after each of these tests and include the output of running your code as a comment in your submission.

We use python for this course

Solutions

Expert Solution

SOURCE CODE:python

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.




def remove_item(dictionary, key):
    # Check if the key exists
    if key in dictionary:
        # Remove the key
        del dictionary[key]
    else:
        # Key doesn't exist
        print('The new item has not been removed because there is no matching key in the dictionary.')


def add_new_item(dictionary, key, value):
    # Check if the key exists
    if key in dictionary:
        print('The new item has not been added because there is already a matching key in the dictionary. ')
    else:
        # Key doesn't exist
        # So, add the key and value
        dictionary[key] = value


# TEST the functions

# create an empty dictionary
dictionary = dict()

print("The dictionary is:", dictionary)
# 1. try removing a key from dictionary
print('1. try removing a key from dictionary')
remove_item(dictionary, 10)
print("\nThe dictionary is:", dictionary)

# 2. add some items
print('# 2. add some items')
add_new_item(dictionary, 'name', 'praveen')
add_new_item(dictionary, 'age', 24)
add_new_item(dictionary, 'city', 'LA')
print("\nThe dictionary is:", dictionary)

# 3. try deleting an existing item
print('# 3. try deleting an existing item')
remove_item(dictionary, 'city')
print("\nThe dictionary is:", dictionary)

# 4. try adding an existing item
print('# 4. try adding an existing item')
add_new_item(dictionary, "name", "Raju")
print("\nThe dictionary is:", dictionary)

===========


Related Solutions

Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Requirements: In this assignment, you are going to create two switch functions. The first function is...
Requirements: In this assignment, you are going to create two switch functions. The first function is going to be called switchVal and the second is going to be called switchRef. The goal of this assignment is to demonstrate the understanding of what pass-by-reference (Links to an external site.) and pass-by-value (Links to an external site.) do when working with functions that you create. The two functions that you will modify need to accept two parameters and inside them they need...
Process each line so you create dictionary where key is name of the company and value...
Process each line so you create dictionary where key is name of the company and value is a net balance #2.a. First column of each row is a name of the company #2.a.a. Utes company has different types of spelling, so you need to put all of them under the same key #2.b. Second column is payments in #2.c. Third column is payments out. Note that they can be negatives as well. # Hint: Absolute value could be derived by...
You can only use built in Lisp functions and you cannot use setq function. Write a...
You can only use built in Lisp functions and you cannot use setq function. Write a function in Lisp called f1 that counts the number of lists in a list. Example: (f1 ‘(a (a b (b c)) c d (e))) returns 2
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values in the dictionary. For example: dict1 = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':4}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} dict2 = {300:{'s': 300}, 400:{'s': 100, 'g': 100, 'p': 100}, 500: {'s': 50 ,'m': 400, 'p':30, 'i': 50}, 600: {'s': 40, 'i': 400}, 700: {'m': 100, 'p': 50}} def dict_sum(db): should give output...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Python pls create a function called search_position. This function returns a dictionary. team1 = {'Fiora': {'Top':...
Python pls create a function called search_position. This function returns a dictionary. team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}} def search_position(team1): should return {'Top': {'Fiora': 1, 'Yasuo':5,'Olaf':3,'Shaco':}, 'Jungle': {'Shaco': 4}, 'Mid': {'Yasuo', 2, 'Fiora': 4,'Olaf':2}, 'Bottom': {'Fiora': 3}, 'Support': {'Olaf': 4}} *******IMPORTANT*************** The result should be alphabetical order.
create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
Python create a function tryhard and print out the dictionary as a string form. For example...
Python create a function tryhard and print out the dictionary as a string form. For example def tryhard(d:dict): #Code here input: d = {'first': {}, 'second': {'1': {'move'}, '0': {'move', 'slow'}}, 'third': {'1': {'stop'}}} output = " first movie: [ ]\n third movie: [('1', ['stop'])]\n second movie: [('0', ['slow', 'move']), ('1', ['move'])]\n"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT