1012-11 stuckkkk

Screen Link: https://app.dataquest.io/m/1012/python-fundamentals-practice-problems/11/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: False

from csv import reader

def check_unique_values(csv_filename,col_name):
    file = open(csv_filename)
    read = reader(file)
    header = list(read)[0]
    lister = list(read)
    lister = lister[1:]
    
    si = find_col_index(csv_filename,col_name)
    if si == -1:
        return None
    freq = {}
    for x in lister:
        if x[si] in freq:
            freq[x[si]] += 1
        else:
            freq[x[si]] = 1
    for key in freq:
        if freq[key] > 1:
            return False
    return True
       


check_unique_values(csv_filename,col_name1)

What I expected to happen:
for the function to work properly lol

What actually happened:

"Your function returned an incorrect answer. Input: ['test.csv', 'h2'] Your answer: True Expected answer: False"

having a lot of trouble of figuring out why everything is coming out true…

The issue is because of the following -

header = list(read)[0]

reader creates an iterator which essentially iterates over the csv file line-by-line.

iterators in Python iterate over something and once an iteration is completed they are exhausted (because they reach the end of what they were iterating over). They then hold nothing in them. (Note: this is a simplified explanation applicable to this scenario)

So, when you use the above code, list(read)[0], the iterator goes through the csv once, all of that gets saved into a list, and then the iterator, read, is empty.

Then, when you use the code -

lister = list(read)

lister will be empty because read is now empty. You can print out lister in your code and it will return a [].

Just remove that line of code for your header and your code works as expected.

Also, please try to make sure your question/post titles are descriptive of the problem you are facing. It’s easier for other students to find them if need be.

1 Like

Hi the_doctor,

thank you for your help! i was really having trouble.

Sorry about the title. I didn’t really understand what the problem was so it was hard for me to think of a descriptive title so I thought just putting the problem number would be enough. I’ll be more creative next time.

1 Like

Thanks for being open to improving the title @violaduck. :slight_smile:

Here’s my suggestion - include the name of the function or method that is causing the error or even the kind of manipulation you are trying to do.

1 Like

Hello
I am using the solution provided by dataquest but still got an error. here is the provided solution; So what’s the problem ? can’t figure it out

def check_unique_values(csv_filename, col_name):
** col_index = find_col_index(csv_filename, col_name)**
** if col_index == -1:**
** return None**
** opened_file = open(csv_filename)**
** read_file = reader(opened_file)**
** rows = list(read_file)**
** rows = rows[1:]**
** freq = {}**
** for row in rows:**
** if row[col_index] in freq:**
** freq[row[col_index]] += 1**
** else:**
** freq[row[col_index]] = 1**
** for key in freq:**
** if freq[key] > 1:**
** return False**
** return True**

print(check_unique_values(csv_filename, col_name1))
print(check_unique_values(csv_filename, col_name2))
print(check_uni

Hi. You will have to create a new post for your question and also share the error you get. I would also recommend formatting your code properly in that new post. Someone can then answer it accordingly.