I need to store data coming from a multiselect dropdown menu and thinking about what data structure is the simplest and best for storage of such values. I had several ideas of how to store the values but I am asking if there is some preferred pythonic way how to solve this (e.g. some class for multiselect structure).
#1)
fruits=["Apple","Pear","Apricot","Banana","Orange","Raspberry","Blueberry","Kiwi","Pineapple"]
chosen_fruit_indices=[2,4,5]
#problem1: need to update indices when fruit list is updated
#2)
fruits=["Apple","Pear","Apricot","Banana","Orange","Raspberry","Blueberry","Kiwi","Pineapple"]
chosen_fruits=["Apricot","Orange","Raspberry"]
#problem2: need to be updated when fruit list does not contain one of chosen items anymore
#3)
fruits=["Apple","Pear","Apricot","Banana","Orange","Raspberry","Blueberry","Kiwi","Pineapple"]
chosen_fruit_dict={"Apricot":True,"Orange":True,"Raspberry":True}
#problem2: need to be updated when fruit list does not contain one of chosen items anymore
#problem3: problematic duplicate keys in dictionary
#4)
chosen_fruit_dict={"Apple":False,"Pear":False,"Apricot":True,"Banana":False,"Orange":True,"Raspberry":True,
"Blueberry":False,"Kiwi":False,"Pineapple":False}
#problem3: problematic duplicate keys in dictionary
#advantage1: one variable is enough to store all the information
In order to solve the question I came up with a new package tackling all: duplicate values; fast subsetting of ticked and unticked items; and ordered adding and removing of items.
It can be found here: https://github.com/DovaX/multiselect
One can easily install it with
pip install multiselect
The usage is the following
from multiselect.multiselect_core import Multiselect
fruits=["Apple","Pear","Apricot","Banana","Orange","Raspberry","Blueberry","Kiwi","Pineapple"]
fruit_multiselect=[{"key":"Apple","value":False},{"key":"Pear","value":False},{"key":"Apricot","value":True},{"key":"Banana","value":False},
{"key":"Orange","value":True},{"key":"Raspberry","value":True},{"key":"Kiwi","value":False},{"key":"Pineapple","value":False}]
multiselect=Multiselect(fruit_multiselect) #1st way how to initialize
multiselect2=Multiselect.init_from_options(fruits) #2nd way how to initialize
multiselect2.tick_by_indices([2,4,5])
multiselect3=Multiselect.init_from_options(fruits) #3rd way how to initialize
multiselect3.tick_all_by_key("Apricot")
multiselect3.tick_all_by_key("Orange")
multiselect3.tick_all_by_key("Raspberry")
multiselect4=Multiselect.init_from_options(fruits) #4th way how to initialize
multiselect4.tick_all_by_keys(["Apricot","Orange","Raspberry"])
print(multiselect.data)
#[{'key': 'Apple', 'value': False}, {'key': 'Pear', 'value': False}, {'key': 'Apricot', 'value': True}, {'key': 'Banana', 'value': False}, {'key': 'Orange', 'value': True}, {'key': 'Raspberry', 'value': True}, {'key': 'Kiwi', 'value': False}, {'key': 'Pineapple', 'value': False}]
print(multiselect.keys())
#['Apple', 'Pear', 'Apricot', 'Banana', 'Orange', 'Raspberry', 'Kiwi', 'Pineapple']
print(multiselect.values())
#[False, False, True, False, True, True, False, False]
print(multiselect)
#<Multiselect object> [{'key': 'Apple', 'value': False}, {'key': 'Pear', 'value': False}, {'key': 'Apricot', 'value': True}, {'key': 'Banana', 'value': False}, {'key': 'Orange', 'value': True}, {'key': 'Raspberry', 'value': True}, {'key': 'Kiwi', 'value': False}, {'key': 'Pineapple', 'value': False}]
print(multiselect["Apple"])
#False
print(multiselect.items())
#[('Apple', False), ('Pear', False), ('Apricot', True), ('Banana', False), ('Orange', True), ('Raspberry', True), ('Kiwi', False), ('Pineapple', False)]
print(multiselect.get_ticked_indices())
#[2, 4, 5]
print(multiselect.get_unticked_indices())
#[0, 1, 3, 6, 7]