Screen Link:
My Code:
# provided inputs
csv_filename = "users.csv"
col_name1 = "name"
# answer to this input: False
col_name2 = "id"
# answer to this input: True
col_name3 = "email"
# answer to this input: True
from csv import reader
def find_col_index(csv_filename, col_name):
opened_file = open(csv_filename)
read_file = reader(opened_file)
data_set = list(read_file)
header = data_set[0]
if col_name in header:
return header.index(col_name)
else:
return -1
def check_unique_values(csv_filename, col_name):
opened_file = open(csv_filename)
read_file = reader(opened_file)
data_set = list(read_file)
col_index = find_col_index(csv_filename, col_name)
#if col_index == -1:
# return 'None'
#else:
dups_list = []
unique_list = []
for row in data_set[1:]:
if row[col_index] in unique_list:
dups_list.append(row[col_index])
else:
unique_list.append(row[col_index])
if dups_list is None:
return True
else:
return False
print(check_unique_values('users.csv', 'name'))
print(check_unique_values('users.csv', 'id'))
print(check_unique_values('users.csv', 'email'))
What I expected to happen:
I expected it to return False, True, ? (not sure if there are duplicate email addresses)
What actually happened:
It returned False, False, False
When I tried to display the dups_list and unique_list, I got an error message saying that they are not defined, but I can’t figure out why that’s so.
I wanted to solve this in a way that captured any duplicates in a list
Paste output/error here