222k views
2 votes
Define a Python function named low_rent that two parameters. The first parameter will be a list. Each entry in the first parameter is also a list of 3 values representing the following fields: State Code, MedianEfficiencyRent, Area Name The first and last of these will be strs and the middle one is a float The second parameter will be a string specifying the CSV file you must append. For each entry in the first parameter, if the median rent of an efficiency (e.g. value at index 1) is less than 576, write the entry as three columns in the CSV file. You do not need to do anything when the median rent of an efficiency is 576 or higher. Examples:low_rent([], "empty.csv") appends a file named empty.txt but does not add any data.

low_rent([ ["NY", 603.0, "Cayuga County, NY"], ["AR", 410.0, "Sevier County"] ], "a.csv") adds the row AR, 410.0, Sevier County at the end of the a.csv file.

User Laurance
by
4.5k points

1 Answer

6 votes

Answer:

def low_rent(list,file): #defining the function

with open(file,'w') as file: #opening the filename with write mode

comma=',' #defining the variable comma to join the list

for i in range(len(list)): #iterating over the list

if(list[i][1])<576: #checking if value at index 1 is less than 576

final_list=[str(i) for i in list[i]] #making all the arguments in list as stirngs

string=comma.join(final_list) #jointing the list with comma

file.write(string) #writing the final string to the file

low_rent([["NY", 603.0, "Cayuga County, NY"],["AR", 410.0, "Sevier County"]],'a.csv')

#call the function

Step-by-step explanation:

User Dave Wood
by
3.9k points