Question

python Define a function called print_values which takes a dictionary object as a parameter. The function...

python

Define a function called print_values which takes a dictionary object as a parameter. The function should print all values in the dictionary. However, the order is based on the sorted keys in the dictionary. For example, if we have the following dictionary:

{'b':36, 'a':12, 'c':24}

The output is:

12 36 24 

A faulty solution has been provided below. Identify the fault and submit a corrected version of this code.

def print_values(dict1):
    for k in list(dict1.keys()).sort(): 
        print(dict1[k], end=" ")

For example:

Test Result
print_values({'b':36, 'a':12, 'c':24})
12 36 24 
print_values({97:'a', 65:'A', 43:'+', 48:'0'})
+ 0 A a 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def print_values(dict1):
        for k in sorted(list(dict1.keys())): 
                print(dict1[k], end=" ")
        print()
        
print_values({'b':36, 'a':12, 'c':24})
print_values({97:'a', 65:'A', 43:'+', 48:'0'})

The sort method sorts the given list in place and returns None.. Hence the for loop fails.. you should use sorted method, which returns the sorted list, on which for loop can work.

please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
python Define a function called print_values which takes a dictionary object as a parameter. The function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of...

    PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of the parameter dictionary are words and the corresponding values are lists of synonyms (synonyms are words which have the same or nearly the same meaning). The function removes all the synonyms which contain the letter 'e' or 'E') from each corresponding list of synonyms. As well, the function sorts each corresponding list of synonyms. Note: the testing code makes use of the print_dict_in_key_order(a_dict) which...

  • # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python...

    # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are...

  • Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys)...

    Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys) to integers (values) and a list of strings. All of the strings in the list are keys to the dictionary. Complete the function such that it returns the subset of the dictionary defined by the keys in the list of strings. For example, with the dictionary ("puffin": 5, "corgi": 2, "three": 3) and the list ("three", "corgi"), your function should return {"corgi": 2, "three"...

  • python Which of the following statements are true? SELECT ALL THAT APPLY A dictionary is not...

    python Which of the following statements are true? SELECT ALL THAT APPLY A dictionary is not iterable. If die is a dictionary, dictionary returns a list of all the keys in the If my_dict is a dictionary, it can be sorted in ascending order of its keys using the dictionary's sort function. If my_dict is a dictionary, and k is a variable, the structure k in my_dict returns True if k is a key in the dictionary.

  • Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings...

    Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings as a parameter. The strings are intended to represent a person's first and last name (with a blank in between). Assume the last names are unique. The function should return a dictionary (dict) whose keys are the people's last names, and whose values are their first names. For example: >>> dictionary = names([ 'Ljubomir Perkovic', \ 'Amber Settle', 'Steve Jost']) >>> dictionary['Settle'] 'Amber' >>>...

  • Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s...

    Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s): dictionary :dictionary reverse boolean, default: False Output(s): .sorted_keys: list Procedure(s) Get the keys from the input dictionary using the keys()method (this will return a list of keys) Use the sorted function to sort the list of keys. Pass in reverse to sorted to set whether to reverse...

  • Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this...

    Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this dictionary each key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course. For example, the following...

  • using python Write a function that takes a dictionary and a list of keys as 2...

    using python Write a function that takes a dictionary and a list of keys as 2 parameters. It removes the entries associated with the keys from the given dictionary Your function should not print or return anything. The given dictionary should be modified after the function is called Note: it means that you can't create another dictionary in your function. Since dictionaries are mutable, you can modify them directly def remove_keys (dict, key_list) "" "Remove the key, value pair from...

  • Iterating Over a Dictionary A dictionary is an iterable object and so a for loop can...

    Iterating Over a Dictionary A dictionary is an iterable object and so a for loop can be used to iterate over the entries in a dictionary. The loop for k in dict: body_code will iterate over the keys of the dictionary dict setting k in turn to each key of dict. Consider a dictionary that has strings as keys and integers as values - e.g. {'a':24, 'e':30, 't':12, 'n':10} Write a function big_keys that takes such a dictionary as the...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT