- 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

25th Mar 2021, 1:26 AM
Alex
Alex - avatar
5 Antworten
+ 3
For that python has the set function: distinct_values = set(value_list)
25th Mar 2021, 1:37 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 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))
25th Mar 2021, 4:28 AM
visph
visph - avatar
0
if print is not allowed then make a function with return statement.
25th Mar 2021, 2:39 AM
iTech
iTech - avatar
0
looks like you may have to do a loop. for value in value_list: distinct_values = len(set(value))
25th Mar 2021, 3:04 AM
you are smart. you are brave.
you are smart. you are brave. - avatar
- 1
Thank you all for your help with my question.
25th Mar 2021, 12:44 PM
Alex
Alex - avatar