I am trying to make a dropdown list with only the unique elements from a column in a table. I tried different solutions but keep ending up with the full list with the same values repeating it self.
the boilerblate:
def drop_down_1_show(self, **event_args):
"""This method is called when the DropDown is shown on the screen"""
searchitem = [(r['Ejendom'],r) for r in app_tables.inst.search()]
self.drop_down_1.items = searchitem
one attemp:
def drop_down_1_show(self, **event_args):
"""This method is called when the DropDown is shown on the screen"""
searchitem = [(r['Ejendom'],r) for r in app_tables.inst.search()]
searchitem = list(dict.fromkeys(searchitem))
self.drop_down_1.items = searchitem
I also tried itterating through the list but it didn’t work either
def drop_down_1_show(self, **event_args):
"""This method is called when the DropDown is shown on the screen"""
searchitemlist = set()
searchitem = [(r['Ejendom'],r) for r in app_tables.inst.search()]
for x in searchitem:
searchitemlist.add(x)
#searchitem = list(dict.fromkeys(searchitem))
self.drop_down_1.items = searchitemlist
also tried
searchitem = [(r['Ejendom'],r) for r in app_tables.inst.search()]
used = []
for x in searchitem:
if x in used:
searchitem.delete(x)
else:
used.append(x)
self.drop_down_1.items = used
A common technique for creating lists of unique values is to create a set and pass that set to the list function.
In your boilerplate example, that might look something like:
def drop_down_1_show(self, **event_args):
"""This method is called when the DropDown is shown on the screen"""
searchitem = list({(r['Ejendom'],r) for r in app_tables.inst.search()})
self.drop_down_1.items = searchitem
(I converted your original list comprehension to a set comprehension and passed that to the list function).
2 Likes
Note: because row r
is present in each tuple, each tuple will be unique, even if r['Ejendom']
is not.
Did you want only distinct values of r['Ejendom']
?
Yes you are right. only distinct values.
I tried making a code in repl that iterates over a list. But this metode dosnt work in anvil.
Thanks alot for the explanation. I was starting to think it might be something like this but I didnt know how to make a solution.
https://repl.it/join/nqpuuvav-wikinggaffa
Thanks for the example! This helps me alot.
I tried some solutions with list and set.
I didn’t get it to work. I still get a list with the same value repeated several times
@p.colbert @owen.campbell
Silly me… I didn’t need r in the line at all. I got it to work. Thanks to your explanations.
This was all that was needed!
def drop_down_1_show(self, **event_args):
searchitem = list(set([(r['Ejendom']) for r in app_tables.inst.search()]))
self.drop_down_1.items = searchitem
1 Like
I wonder if a set comprehension would suffice here. That is, instead of
list(set([(r['Ejendom']) for r in app_tables.inst.search()]))
how about
list( { r['Ejendom'] for r in app_tables.inst.search() } )
? This should build a set
of strings directly, instead of through an intermediate list
.