- 1
If I have a variable that is a list how do I assign the number of nonduplicate values to a variable in Python? Homework
I am trying to solve this Python homework question in MyProgrammingLab that I have: Given the list value_list, assign the number of nonduplicate values to the variable distinct_values. Here are the answers I need to get: https://imgur.com/a/lX3g1kR Is there an easy way to solve this? My programmingLab says I shouldn't be using print or insert statements https://pastebin.com/dVi20Lfp
5 Answers
+ 3
For that python has the set function:
distinct_values = set(value_list)
+ 3
Benjamin JĂŒrgens answer is right...
however, this give you a set, not the length of the set ^^
to get count of distinct values, you should do:
distinct_values_count = len(set(value_list))
0
if print is not allowed then make a function with return statement.
0
looks like you may have to do a loop.
for value in value_list:
distinct_values = len(set(value))
- 1
Thank you all for your help with my question.