Generally to assign/return boolean values, instead of doing this:
my_result = False
if condition1 and condition2:
my_result = True
else:
my_result = False
You can simply do:
my_result = condition1 and condition2
Back to your question of creating sets.
You can create set
from a list (or any iterable object) like below:
your_list = [1,2,3,4]
your_set = set(your_list)
if (len(your_set)==len(your_list):
pass #All elements are unique
The primary property of a set is that it doesn't have duplicates. So if the length of your list and your set match then we can deduce that all elements in your list are unique.
You can check if all elements are in ascending order by first sorting the list and then matching if your sorted list is the same as your original list. If that's the case, then you can decude that all elements in your list are in ascending order.
your_list = [1,3,2,4]
sorted_list = sorted(your_list) #[1,2,3,4]
descending_sorted_list = sorted(your_list, reverse=True) #[4,3,2,1]
if (sorted_list == your_list):
pass #They are in ascending order
Joining these two conditions with an AND
operators validates both the conditions.
#This sets a boolean (True/False)
result = (sorted_list == your_list) and (len(your_set)==len(your_list))
Similarly, in a method context you can return it directly:
return (sorted_list == your_list) and (len(your_set)==len(your_list))