Python Fundamentals Practice Problem p. 6

Screen Link:

https://app.dataquest.io/m/1012/python-fundamentals-practice-problems/6/checking-unique-column-values

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

Hi @jdiana

I wrote the code as you want to check duplicates and it works fine.

Here it is

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
    unique_lst = []
    dups_lst = []
    for each in data_set[1:]:
        if each[col_index] in unique_lst:
            dups_lst.append(each[col_index])
        else:
            unique_lst.append(each[col_index])
            
    if len(dups_lst) == 0:
        return True
    else:
        return False

And the empty list does not mean that it is None. You can check it through this code:

x = []
if x is None:
    print('None')
else:
    print('Something else')

I hope this helps :slightly_smiling_face: .

Thank you!!

I was treating “is None” like is null in sql, but I can see that the empty list is more like an empty character string, which isn’t really null.

Thanks for you quick response.

Jim

1 Like