I am so proud of myself, thank you codecademy, I did it and I can do that.
So my working example:
def unique_values(my_dictionary):
seen_values = []
for value in my_dictionary.values():
if value in seen_values:
continue
else:
seen_values.append(value)
return len(seen_values)
def unique_values(my_dictionary):
seen_values = []
for value in my_dictionary.values():
if value == seen_values:
continue
else:
seen_values.append(value)
return len(seen_values)
tryna understand why this isn’t working, it seems logical to me.
The hint given for this challenge isn’t entirely correct or at least misses out a step.
I did the following which returned the correct output:
def unique_values(my_dictionary):
seen_values =
counter = 0
for value in my_dictionary.values():
if value in seen_values:
continue
if value not in seen_values:
seen_values.append(value)
counter += 1
return counter
As you can see I added in a counter and returned the counter instead of seen_values which gave the correct answer for this challenge.
The hint is correct for this challenge. In fact, it isn’t necessary to use a counter to solve this challenge. Take a look at this snippet of your code.
What you are doing here is you are appending each value to seen_values if it is not already in seen_values. Then, you are incrementing the counter by 1.
Take the following argument: [0, 0, 4, 2, 9, 9]. After your code executes, counter will have a value of 4, since it will increment by one for iterations where 0 (the first time), 4, 2, and 9 (the first time) are the value of value. However, these same values will also be appended to seen_values, meaning the length of seen_values (4) is the same as the value of counter (also 4).
In sum, each time you increment counter by 1, you are also appending an element to seen_values, making len(seen_values) == counter.