Question

PYTHON PROBLEM Define a function generate_kv_strings( dictionary ) that creates and return a list where the...

PYTHON PROBLEM

Define a function generate_kv_strings( dictionary ) that creates and return a list where the element is a string representing a key-value pair in the format "key: value". Your solution must use a list comprehension.

Hint: Remember that you need to convert each key and value into a str before concatenating them, in case they are of a different type.

Example Usage:
states = {'Florida': 'FL', 'Oregon': 'OR', 'California': 'CA', 'New York': 'NY', 'North Carolina': 'NC'}
numbers = {'four': 4, 'three': 3, 'two': 2, 'one': 1}
print generate_kv_strings( states )
print generate_kv_strings( numbers )
Run > Run Module
['California: CA', 'New York: NY', 'Florida: FL', 'North Carolina: NC', 'Oregon: OR']
['four: 4', 'one: 1', 'three: 3', 'two: 2']

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# python code to a list where the element is a string representing a key-value pair in the format "key: value"

def generate_kv_strings(states):
   list = []
   # iterate over dictionary
   for key in states:
      # create a string of key value pair
      string = str(key) + ": " + str(states[key])
      # append to list
      list.append(string)
   return list


states = {'Florida': 'FL', 'Oregon': 'OR', 'California': 'CA', 'New York': 'NY', 'North Carolina': 'NC'}
numbers = {'four': 4, 'three': 3, 'two': 2, 'one': 1}
print generate_kv_strings(states)
print generate_kv_strings( numbers )

'''
output:

['California: CA', 'New York: NY', 'Florida: FL', 'North Carolina: NC', 'Oregon: OR']
['four: 4', 'one: 1', 'three: 3', 'two: 2']

'''

Add a comment
Know the answer?
Add Answer to:
PYTHON PROBLEM Define a function generate_kv_strings( dictionary ) that creates and return a list where the...
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
  • JAVA: File USCapitals.txt is here: Alabama, Montgomery Alaska, Juneau Arizona, Phoenix Arkansas, Little Rock California, Sacramento...

    JAVA: File USCapitals.txt is here: Alabama, Montgomery Alaska, Juneau Arizona, Phoenix Arkansas, Little Rock California, Sacramento Colorado, Denver Connecticut, Hartford Delaware, Dover Florida, Tallahassee Georgia, Atlanta Hawaii, Honolulu Idaho, Boise Illinois, Springfield Indiana, Indianapolis Iowa, Des Moines Kansas, Topeka Kentucky, Frankfort Louisiana, Baton Rouge Maine, Augusta Maryland, Annapolis Massachusettes, Boston Michigan, Lansing Minnesota, Saint Paul Mississippi, Jackson Missouri, Jefferson City Montana, Helena Nebraska, Lincoln Nevada, Carson City New Hampshire, Concord New Jersey, Trenton New York, Albany New Mexico, Santa Fe...

  • I am trying to do this assignment: Write a program that reads a list of concepts/strings...

    I am trying to do this assignment: Write a program that reads a list of concepts/strings from the LIST.txt text file (one concept per line), stores them into an array of strings, and then prints the following: 1.Prints a heading “LIST1:” and then prints all the concepts from the array, one concept per line 2.Prints a heading “LIST2:” and then prints all the concepts from the array that do not contain spaces, one concept per line 3.Prints a heading “LIST3:”...

  • With the multiple linear regression equation in (2), what will be the alumni-giving rate with the...

    With the multiple linear regression equation in (2), what will be the alumni-giving rate with the graduation rate as 85%, 60% of classes with fewer than 20 students, and student-faculty ratio as 12? (20%). State Graduation Rate 85 79 93 85 75 Alumni Giving Rate 25 % of Classes Student-Faculty Under 20 Ratio 39 13 68 8 60 8 33 40 65 3 10 46 28 67 72 52 8 31 89 90 45 69 72 12 7 13 10...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Business Statistics

    Alumni donations are an important source of revenue for college and universities. If administrators could determine the factors that could lead to increases in thepercentage of alumni who make a donation, they might be able to implement policies that could lead to increased revenues. Research shows that students who are moresatisfied with their contact with teachers are more likely to graduate. As a result, one might suspect that smaller class sizes and lower student-faculty ratios mightlead to a higher percentage...

  • Write a French/English dictionary lookup program. Read a list of pairs of English and French words...

    Write a French/English dictionary lookup program. Read a list of pairs of English and French words from a file specified by the user. English/French words should be exact matches (don't try to find partial matches). Use the supplied EnglishFrenchDictionary.java class as your main class. Fill in the missing code in the DictionaryTable.java class to read the input file and perform the searches. Add code to the DictionaryTable read() method to: read pairs of lines (English word is on the first...

  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

  • JAVA Create a Governor class with the following attributes: name : String party - char (the...

    JAVA Create a Governor class with the following attributes: name : String party - char (the character will be either D, R or I) ageWhenElected : int Create a State class with the following attributes: name : String abbreviation : String population : long governor : Governor Your classes will have all setters, getters, typical methods to this point (equals(), toString()) and at least 3 constructors (1 must be the default, 1 must be the copy). You will be turning...

  • State POVERTY% GUN DEATHS H.S. GRAD's UNEMPLOYMENT New Jersey 6.80% 5.7 91.8 91.8...

    State POVERTY% GUN DEATHS H.S. GRAD's UNEMPLOYMENT New Jersey 6.80% 5.7 91.8 91.8 Vermont 7.60% 9.2 91 3.6 91 3.6 Minnesota 8.10% 12 91.5 4 Hawaii 8.60% 2.6 87.4 4.9 Delaware 9.20% 10.3 90.4 3.7 Utah 9.20% 12.6 90.4 3.7 Virginia 9.20% 10.2 86.6 4.5 Nebraska 9.50% 9 89.8 2.8 connecticut 9.70% 4.4 88.6 5.3 Maryland 9.70% 9.7 88.2 5.1 Idaho Alaska 9.90% 10.00% 14.1 17.6 88.4 91.4 4.2 6.6 Massachusetts 10.10% 3.1 89 4.7 Washington 10.20% 8.7 89 4.7...

  • Please write answers clear. I will leave like if its correct. State POVERTY% GUN DEATHS H.S. GRAD's UNEMPLOYMENT New Jersey 6.80% 5.7 91.8 91.8 Vermont 7.60%...

    Please write answers clear. I will leave like if its correct. State POVERTY% GUN DEATHS H.S. GRAD's UNEMPLOYMENT New Jersey 6.80% 5.7 91.8 91.8 Vermont 7.60% 9.2 91 3.6 91 3.6 Minnesota 8.10% 12 91.5 4 Hawaii 8.60% 2.6 87.4 4.9 Delaware 9.20% 10.3 90.4 3.7 Utah 9.20% 12.6 90.4 3.7 Virginia 9.20% 10.2 86.6 4.5 Nebraska 9.50% 9 89.8 2.8 connecticut 9.70% 4.4 88.6 5.3 Maryland 9.70% 9.7 88.2 5.1 Idaho Alaska 9.90% 10.00% 14.1 17.6 88.4 91.4 4.2...

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