Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import csv, jsonalist = [[Legends,Nickname], [Kobe Bryant, Mamba], [Shaq, Shaq Daddy], [Calvin Johnson, Megatron]] Question 1 - Return a list of dictionaries with the keys

import csv,
jsonalist = [["Legends","Nickname"], ["Kobe Bryant", "Mamba"], ["Shaq", "Shaq Daddy"], ["Calvin Johnson", "Megatron"]]
Question 1 - Return a list of dictionaries with the keys being the fieldnames and the values being the respective values in each row - The parameter alist is a list of lists, with the 0th index being the fieldnames - DO NOT HARD CODE the keys to each dictionary, we will test your code with different data
- e.g
alist = [["Legends","Nickname"], ["Kobe Bryant", "Mamba"], ["Shaq", "Shaq Daddy"], ["Calvin Johnson", "Megatron"]]
print(list_of_lists_to_list_of_dicts(alist))
[{'Legends': 'Kobe Bryant', 'Nickname': 'Mamba'}, {'Legends': 'Shaq', 'Nickname': 'Shaq Daddy'}, {'Legends': 'Calvin Johnson', 'Nickname': 'Megatron'}]
def list_of_lists_to_list_of_dicts(alist):
Question 2 - Write a function that writes the header and each row of data to the csv file name specified in the parameter. - Return the number of rows written to the file, NOT including the header - The parameters alist follows the same structure as alist in Question1, filename will include the file extension (e.g. "filename.csv")
- Hint: call list_of_lists_to_list_of_dicts to convert the list of lists to list of dicts before writing using csv.DictWriter
- e.g.
alist = [["Legends","Nickname"], ["Kobe Bryant", "Mamba"], ["Shaq", "Shaq Daddy"], ["Calvin Johnson", "Megatron"]]
print(list_of_lists_to_csv(alist, 'output.csv')
The example csv should look like the following:#
Legends,Nickname
Kobe Bryant,Mamba
Shaq,Shaq Daddy
Calvin Johnson,Megatron
def list_of_lists_to_csv(alist, filename):
Question 3 - Given the following data, ...names = [("Tom", "Brady"), ("Drew", "Brees"), ("Aaron", "Rogers"), ("Joe", "Montana"), ("Dan", "Marino")]ages = [42, 40, 36, 63, 58]
- Combine the names and ages to a list of lists in the following format
- [["fname", "lname","age"], ["Tom", "Brady", 42] ... ] - Convert the list of lists to list of dictionaries using list_of_lists_to_list_of_dicts
- Create a json formatted dictionary that with the following format: The keys for the outermost dictionary should be 'Retired' and 'Active'. A quarterback is considered 'Retired' if their 'age' is greater than 40, and 'Active' otherwise. Refer to the example for the proper format
using python

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. Where will you recommend that she hold the focus group?

Answered: 1 week ago

Question

3. What might you have done differently

Answered: 1 week ago